Leetcode-python-常用代码

Collections 包

from collections import Counter, deque, defaultdict
  1. collections.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 SortedList
  1. SortedList
    • 常用于模拟单调队列(python中没有内置实现的有序容器)