Leetcode

Grammar

Internal functions

  • max, sum, map
  • reversed, sorted

Libraries

  • heapq

    import heapq
    heapq.heapify(heap)
    heapq.heappush(heap, v) # Min heap
    heapq.heappush(heap, -v) # Max heap
    heapq.heappush(heap, (v1, v2, v3))
    heapq.heappop(heap)
    
  • re

    import re
    re.split("[!?',;. ]", paragraph.lower())
    re.sub("[!?',;. ]+", " ", paragraph.lower())
    re.replace(' ', '', paragraph)
    
  • random

    import random
    random.randrange(a, b) # a <= N < b
    random.randint(a, b) # a <= N <= b
    
  • string

    chr(65) # 'A'
    ord('B') - ord('A') # 1
    "Hello".split() # ["Hello"]
    "Hello World".split() # ["Hello", "World"]
    list("Hello") # ['H', 'e', 'l', 'l', 'o']
    "Hello".replace("o", "a") # "Hella"
    

Collections

  • defaultdict

    from collections import defaultdict
    shown = defaultdict(int)
    shown = defaultdict(list)
    shown = defaultdict(dict)
    
  • Counter

    from collections import Counter
    counter = Counter(paragraph)
    items = list(counter.items())
    items.sort(key = lambda x: (x[1], x[1]))
    
  • deque

    from collections import deque
    queue = deque("0")
    queue.append("1")
    queue.appendleft("2")
    queue.popleft()
    queue.pop()
    len(queue)
    

Table of contents