Python - Data Type
Dict
Functions
- len(d) 测长度
- del d[key]: remove key and associated value from dictionary d
- key in d: return true if key exists in dictionary
- d1.update(d2)
Traversal of a dict
- dict.items() -> list of (key, value) paris
- dict -> list of keys
- dict.values() -> list of values
Defaultdict
- How does collections.defaultdict work?
The defaultdict will create a default item (according to your definition) when you try to get an item with a key that is not currently in the dictionary.intDict = defaultdict(int) # default item = 0 listDict = defaultdict(list) # default item = []
- self-defined class + defaultdict
class GNode(object): def __init__(self): self.inDegree = 0 self.outNode = []
- Use self.inDegree rather than inDegree.
import collections from defaultdict graph = defaultdict(GNode)
- Pay attention to the spelling of defaultdict
Ordereddict
Set
Functions
- s.add(elem) 添加一项
- s.update(l/s2)
OR
s1 |= l/s2 添加多项 ??? - len(s) set的长度
- x in s
OR
x not in s 测试x是否是/不是s的成员 - s1.issubset(s2)
OR
s1 <= s2 测试是否s1中的每一个元素都在s2中 - s1.issuperset(s2)
OR
s1 >= s2测试是否s2中的每一个元素都在s1 - s1.union(s2)
OR
s1 | s2 求并集 - s1.intersection(s2)
OR
s1 & s2 求交集 - s1.difference(s2)
OR
s1 - s2 返回一个新的set包含s1中有但是s2中没有的元素 - s1.symmetric_difference(s2)
OR
s1 ^ s2 返回一个新的set包含s1和s2中不重复的元素 - s.remove(elem) 从s中删除元素,如果不存在则引发KeyError
- s.discard(elem) 如果在s中存在元素elem,则删除
- s.clear() 删除s中的所有元素
Leetcode example
128. Longest Consecutive Sequence
Why we use set
we want to inquire an element in O(1) and do not need key
Main idea
We need run in O(n), which means we only run once for each consecutive elements sequence.
How to guarantee we only run once for each sequence? The solution is we only traverse the sequence from its end points (smallest or biggest value).
Therefore, only when num-1 not in the set, i.g., num is the smallest value of a sequence, we traverse the sequence and find the maximum value.
Code
class Solution(object):
def longestConsecutive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums = set(nums)
longestNum = 0
for num in nums:
if num-1 not in nums:
currElement = num
currNum = 1
while currElement + 1 in nums:
currNum += 1
currElement += 1
if currNum > longestNum:
longestNum = currNum
return longestNum
List
Functions
i. Acquire information
- len(l)
- index = l.index(num): return the index position of num in list l
ii. 添加元素
- l.append(elem) 在队尾添加元素
- l.insert(pos,elem): insert element elem at the ith position in list l
iii. 删除元素
- l.pop(pos) 可作过程,可作函数
当pos为空时,取出并删除队尾元素
当pos不为空时,取出并删除指定位置元素 - l.remove(elem) 删除指定元素(若有多个,只删除一个)
iv. 倒转
- l.reverse()
例:x=[1,2,3,4,5]
x.reverse() -> [5,4,3,2,1]
v. 排序
- l.sort() / sorted(l) 排序(升序)
- l.sort(reverse=True) / sorted(l,reverse=True) 排序(降序)
- explore more details about sort
vi. 最大与最小
- min(l) / l.min() -> return the minimum number
- max(l) / l.max() -> return the maximum number
vii. 合并
- s.join(l): use str to join the list
viii. 计数
- l.count(target) -> 输出l中target的个数
Note
-
Define 2D array
Correct
a = [[0]*n for i in range(m)]
Incorrect
a = [[0]*n]*m ####WRONG!!!!!!! #The adress of m rows are the same
Deque
from collections import deque
nodeOrder = deque()
nodeOrder.append(node1) # append a new node to tail
nodeOrder.appendleft(node2) # append a new node to head
nodeOrder.pop() # pop from tail
nodeOrder.popleft() # pop from head
## Topological ordering
Leetcode example: 210. Course Schedule II
String
Functions
- len(str) 测量string长度
- str = str.split(str2) 将string中以str2相隔的部分分离成列表
特例:str.split() 将string中以空格相隔的部分分离成列表 student.split() ->
[‘your_first_name’,’your_last_name’,’your_student_ID’] - str = str.lower() 将string中的字母全部变为小写
- str = str.upper() 将string中的字母全部变为大写
- str = str.capitalize() 若string的第一个字符为字母,则将其变为大写
- str = str.replace(str1, str2) 将str中的子串str1替换为str2
Substring
- s[-1:] -> the last character of s
- s[:] -> copy the string s and generate a string with new address
String Matching Problem
Leetcode example: 28. Find the Index of the First Occurrence in a String
Approach 1: Sliding Window
Iterate every position of the original string, and take each position as a beginning.
Approach 2: Rabin-Karp Algorithm (Single Hash)
Main idea: use hash value to detect part of unequal strings in O(1)
“If two hash values are NOT equal, then strings will NOT be equal”
Algorithm
- compare hash value of the substring and the target
- If two hash values are NOT equal, skip
- If two hash values are equal, compare two strings
Key point: how can we reduce the chance of spurious hits?
- large RADIX (larger than the number of unique characters):
pos_1 * RADIX + pos_2 * RADIX ^ 2 + … + pos_n * RADIX ^ n - use MOD to reduce overflow, e.g.,
MOD = 10**9+33 MOD = 2**31-1
- update the hash value in O(1)
Approach 3: Double Hash
Main idea: use two hash methods to essentially reduce the chance of spurious hits.
Use two different mods:
- MOD_1 = 10**9 + 33
- MOD_2 = 2**31 - 1