目录
注意:
1. 两数之和
187. 重复的DNA序列
706. 设计哈希映射
652. 寻找重复的子树
560. 和为K的子数组
547. 朋友圈
684. 冗余连接
692. 前K个高频单词
和大雪菜刷题https://www.bilibili.com/video/BV1T441167eG
思路:
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashtable = {}
for index, num in enumerate(nums):
if (target-num) in hashtable:
return [hashtable[target-num], index]
hashtable[num] = index
思路:
class Solution:
def findRepeatedDnaSequences(self, s: str) -> List[str]:
res = []
hash_tabel = {}
for i in range(len(s)-9):
tmp = s[i:i+10]
hash_tabel[tmp] = hash_tabel.get(tmp, 0) + 1
if hash_tabel[tmp] == 2:
res.append(tmp)
return res
思路:
class MyHashMap:
def __init__(self):
"""
Initialize your data structure here.
"""
self.hash = [[] for _ in range(20011)] # 开辟一个大数组,长度为质数,注意这里不能用 [[]] * 20011
# 一般定义成离2的整次幂比较远的质数,这样取模之后冲突的概率比较低。
def put(self, key: int, value: int) -> None:
"""
value will always be non-negative.
"""
t = key % 20011
for item in self.hash[t]: # 遍历哈希到的链表中,查找key,并更新值
if item[0] == key:
item[1] = value
return # 更新完之后,直接返回
self.hash[t].append([key, value]) # 如果链表中找不到对应的key,将其新添到链表中
def get(self, key: int) -> int:
"""
Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key
"""
t = key % 20011
for item in self.hash[t]:
if item[0] == key:
return item[1]
return -1 # 可能哈希的位置,所对应的链表不为空,但是不存在该值
def remove(self, key: int) -> None:
"""
Removes the mapping of the specified value key if this map contains a mapping for the key
"""
t = key % 20011
delete = []
for item in self.hash[t]:
if item[0] == key:
delete = item # remove方法,这里可以偷懒,把对应的value值设为-1,即表示它已经删除
if delete:
self.hash[t].remove(delete)
思路:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def __init__(self):
self.hashtabel = {}
self.count = {}
self.cnt = 0
self.ans = []
def findDuplicateSubtrees(self, root: TreeNode) -> List[TreeNode]:
self.cnt += 1
self.hashtabel["#"] = self.cnt
self.dfs(root)
return self.ans
def dfs(self, root):
if not root:
return str(self.hashtabel["#"])
left = self.dfs(root.left)
right = self.dfs(root.right)
tree = str(root.val) + "," + left + "," + right
if tree not in self.hashtabel:
self.cnt += 1
self.hashtabel[tree] = self.cnt
t = self.hashtabel[tree]
self.count[t] = self.count.get(t, 0) + 1
if self.count[t] == 2:
self.ans.append(root)
return str(t)
思路:
class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
hashtabel = {}
hashtabel[0] = 1
n = len(nums)
acc,cnt = 0, 0
for i in range(n):
acc += nums[i]
if acc-k in hashtabel:
cnt += hashtabel[acc-k]
if acc in hashtabel:
hashtabel[acc] += 1
else:
hashtabel[acc] = 1
return cnt
思路:
class Solution:
def findCircleNum(self, M: List[List[int]]) -> int:
n = len(M)
father = [0] * n
for i in range(n):
father[i] = i
res = n
for i in range(n):
for j in range(i):
if M[i][j]==0:
continue
if self.find(i, father) != self.find(j, father):
father[self.find(i, father)] = self.find(j, father)
res -= 1
return res
def find(self, x, father):
if father[x] != x:
father[x] = self.find(father[x], father)
return father[x]
思路:
class Solution:
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
n = len(edges)
father = [0]*(n+1)
for i in range(1, n+1):
father[i] = i
for edge in edges:
a, b = edge[0], edge[1]
if self.find(father, a) == self.find(father, b):
return [a, b]
father[self.find(father, a)] = self.find(father, b)
def find(self, father, x):
if father[x]!=x:
father[x] = self.find(father, father[x])
return father[x]
思路:
class Solution:
def topKFrequent(self, words: List[str], k: int) -> List[str]:
count = collections.Counter(words)
heap = [(-freq, word) for word, freq in count.items()]
heapq.heapify(heap)
return [heapq.heappop(heap)[1] for _ in range(k)]