- 1. Implement a Hash Table
- 2. 347. Top K Frequent Elements
- 3. 268. Missing Number
- 4. 349. Intersection of Two Arrays
- 5. Find common elements in 3 sorted Arrays
A hash table (hash map) is a data structure that implements an associative array abstract data type, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found.
- Hash Fucntion
- Hash Collision
- Separate chaining
- Open addressing
Implement a Hash Table
TODO
347. Top K Frequent Elements
1 | class Solution(object): |
268. Missing Number
1 | class Solution(object): |
349. Intersection of Two Arrays
Time : O(m + n), Space : O(m + n)
1 | class Solution(object): |
Follow up : two sorted array
- Binary Search, Time : O(mlogn), Space : O(1)
- Two pointers, Time : O(m + n), Space : O(1)
Find common elements in 3 sorted Arrays
Solution1: three Pointers
How to move each pointerSolution2: find common elements a1,a2 then a3
Counter
299. Bulls and Cows
We usually use hash table to do things like counter!1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17class Solution(object):
def getHint(self, secret, guess):
bulls = 0
for ch1, ch2 in zip(secret, guess):
if ch1 == ch2:
bulls += 1
cnt = [0] * 10
for ch in secret:
cnt[ord(ch)-ord('0')] += 1
common = 0
for ch in guess:
if cnt[ord(ch)-ord('0')] != 0:
common += 1
cnt[ord(ch)-ord('0')] -= 1
cows = common - bulls
return str(bulls) + "A" + str(cows) + "B"