最佳答案
I am new to python, and i read some code snippet from some place. It's an implementation of counting sort.
The code is as below:
from collections import defaultdict
def sort_colors(A):
ht = {} # a hash map
ht = defaultdict(lambda:0, ht) # with default value 1
for i in A:
ht[i] += 1
ret = []
for k in [0, 1, 2]:
ret.extend([k]*ht[k])
return ret
As in the first two lines of the func, it's
ht = {}
ht = defaultdict(lambda:0, ht)
I am not quite clear about this initialization.Could you kindly help me figure it out? and also, Shall we just replace these two lines with following?
ht = defaultdict(int) # default value 0