Leetcode-python-常用代码
Collections 包
from collections import Counter, deque, defaultdictcollections.Counter()把输入的变量计数,一般输入的是list或者str。常用于统计 XX 出现的频率,输出是一个字典 比如>>> Counter('asbasb') {'a': 2, 's': 2, 'b': 2} >>> Counter([1, 2, 3, 4, 1, 2]) {1: 2, 2: 2, 3: 1, 4: 1} >>> Counter(['happy','sad','sad']) {'happy':1, 'sad':2}
sortedcontainers 包
from sortedcontainers import SortedListSortedList- 常用于模拟单调队列(python中没有内置实现的有序容器)