File tree Expand file tree Collapse file tree 1 file changed +2
-2
lines changed
docs/19_python内置常用算法和数据结构 Expand file tree Collapse file tree 1 file changed +2
-2
lines changed Original file line number Diff line number Diff line change 29
29
如果你使用 python2 or python3 刷题(比如力扣leetcode),有一些坑或者技巧需要注意:
30
30
31
31
- python3 和 python2 的 dict 有所用不同,python3.7 之后的 dict 会保持插入顺序(不是字典序), python2 不要依赖 dict 迭代顺序,请使用 OrderedDict
32
- - 正确初始化一个二维数组 :` dp = [[0 for _ in range(col)] for _ in range(row)] ` ,不要用 ` dp = [[0] * n] * m ` , 否则里边都
33
- 引用的同一个 list,修改一个都会变。` [[val]*cols for _ in range(rows )] ` 也行
32
+ - 正确初始化一个不可变对象的二维数组 :` dp = [ [0]*col for _ in range(row) ] ` ,不要用 ` dp = [[0] * n] * m ` , 否则里边都
33
+ 引用的同一个 list,修改一个都会变。` [[0 for _ in range(col)] for _ in range(row )] ` 也可以(稍慢),因为数字 0 是不可变对象
34
34
- python在数值范围建议用:` MAXINT = 2**63-1; MININT = -2**63 ` 。因为 python2 sys.maxint 和 python3 sys.maxsize 不统一
35
35
- 优先级队列:使用内置queue.PriorityQueue or heapq ,定义一个 Item 类实现"小于" 魔术方法就可以实现,下边有代码演示
36
36
- python3 的 functools 模块自带了 cache(等价于lru_cache(maxsize=None)) 和 lru_cache 装饰器,在一些需要递归记忆化搜索的时候会很方便
You can’t perform that action at this time.
0 commit comments