Skip to content

Commit b0cc071

Browse files
committed
706_Design_HashMap
1 parent 662ee2e commit b0cc071

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ Also, there are open source implementations for basic data structs and algorithm
151151
| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/617_Merge_Two_Binary_Trees.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/617_Merge_Two_Binary_Trees.java) | Traverse both trees Recursion & Iterative (stack) |
152152
| 654 | [Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/654_Maximum_Binary_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/654_Maximum_Binary_Tree.java) | 1. Divide and conquer, recursive, O(n^2)<br>2. Monotonic stack, O(n) |
153153
| 697 | [Degree of an Array](https://leetcode.com/problems/degree-of-an-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/697_Degree_of_an_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/697_Degree_of_an_Array.java) | 1. Find degree and value, then find smallest subarray (start and end with this value), O(n) and O(n)<br>2. Go through nums, remember left most pos and right most for each value, O(n) and O(n) |
154+
| 706 | [Design HashMap](https://leetcode.com/problems/design-hashmap/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/706_Design_HashMap.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/@706_Design_HashMap.java) | Hash implementation, mod is fine. Be careful about key conflict and key remove. |
154155
| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/709_To_Lower_Case.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/709_To_Lower_Case.java) | String processing:<br>1. str.lower() or str.toLowerCase()<br>2. ASCII processing. O(n) and O(1) |
155156
| 716 | [Max Stack](https://leetcode.com/problems/max-stack/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/716_Max_Stack.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/716_Max_Stack.java) | 1. Two stacks<br> 2. Double linked list and Hash |
156157
| 720 | [Longest Word in Dictionary](https://leetcode.com/problems/longest-word-in-dictionary/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/720_Longest_Word_in_Dictionary.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/720_Longest_Word_in_Dictionary.java) | 1. Brute Force, O(sum(w^2)) and O(w)<br>2. Tire Tree, O(sum(w) and O(w))<br>3. Sort and word without last char, O(nlogn + sum(w)) and O(w) |

create_empty_files.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import sys
2+
3+
if __name__ == '__main__':
4+
file_name = 'test'
5+
try:
6+
file_name = sys.argv[1]
7+
except IndexError:
8+
print("Usage: python create_empty_file [filename]")
9+
print("Creating " + file_name + "in java and python dir...")
10+
with open("python/" + file_name + ".py", 'w'):
11+
pass
12+
with open("java/" + file_name + ".java", 'w'):
13+
pass
14+
print("Done!")

java/706_Design_HashMap.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class MyHashMap {
2+
final ListNode[] nodes = new ListNode[10000];
3+
// https://leetcode.com/problems/design-hashmap/discuss/152746/Java-Solution
4+
public void put(int key, int value) {
5+
int i = idx(key);
6+
if (nodes[i] == null)
7+
nodes[i] = new ListNode(-1, -1);
8+
ListNode prev = find(nodes[i], key);
9+
if (prev.next == null)
10+
prev.next = new ListNode(key, value);
11+
else prev.next.val = value;
12+
}
13+
14+
public int get(int key) {
15+
int i = idx(key);
16+
if (nodes[i] == null)
17+
return -1;
18+
ListNode node = find(nodes[i], key);
19+
return node.next == null ? -1 : node.next.val;
20+
}
21+
22+
public void remove(int key) {
23+
int i = idx(key);
24+
if (nodes[i] == null) return;
25+
ListNode prev = find(nodes[i], key);
26+
if (prev.next == null) return;
27+
prev.next = prev.next.next;
28+
}
29+
30+
int idx(int key) { return Integer.hashCode(key) % nodes.length;}
31+
32+
ListNode find(ListNode bucket, int key) {
33+
ListNode node = bucket, prev = null;
34+
while (node != null && node.key != key) {
35+
prev = node;
36+
node = node.next;
37+
}
38+
return prev;
39+
}
40+
41+
class ListNode {
42+
int key, val;
43+
ListNode next;
44+
45+
ListNode(int key, int val) {
46+
this.key = key;
47+
this.val = val;
48+
}
49+
}
50+
}

python/706_Design_HashMap.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
class MyHashMap(object):
2+
3+
# https://leetcode.com/problems/design-hashmap/discuss/152746/Java-Solution
4+
def __init__(self):
5+
"""
6+
Initialize your data structure here.
7+
"""
8+
self.size = 10000
9+
self.nodes = [None] * self.size
10+
11+
def put(self, key, value):
12+
"""
13+
value will always be non-negative.
14+
:type key: int
15+
:type value: int
16+
:rtype: void
17+
"""
18+
index = hash(key) % self.size
19+
if self.nodes[index] is None:
20+
self.nodes[index] = ListNode(-1, -1)
21+
prev = find(self.nodes[index], key)
22+
if prev.next is None:
23+
prev.next = ListNode(key, value)
24+
else:
25+
prev.next.val = value
26+
27+
def get(self, key):
28+
"""
29+
Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key
30+
:type key: int
31+
:rtype: int
32+
"""
33+
index = hash(key) % self.size
34+
if self.nodes[index] is None:
35+
return -1
36+
prev = find(self.nodes[index], key)
37+
if prev.next is None:
38+
return -1
39+
else:
40+
return prev.next.val
41+
42+
def remove(self, key):
43+
"""
44+
Removes the mapping of the specified value key if this map contains a mapping for the key
45+
:type key: int
46+
:rtype: void
47+
"""
48+
index = hash(key) % self.size
49+
if self.nodes[index] is None:
50+
return
51+
prev = find(self.nodes[index], key)
52+
if prev.next is None:
53+
return
54+
prev.next = prev.next.next
55+
56+
57+
def find(bucket, key):
58+
# find prev node of this key
59+
node = bucket
60+
prev = None
61+
while node is not None and node.key != key:
62+
prev = node
63+
node = node.next
64+
return prev
65+
66+
67+
# Basic node in hash map
68+
class ListNode():
69+
70+
def __init__(self, key, val):
71+
self.key = key
72+
self.val = val
73+
self.next = None
74+
75+
76+
# Your MyHashMap object will be instantiated and called as such:
77+
# obj = MyHashMap()
78+
# obj.put(key,value)
79+
# param_2 = obj.get(key)
80+
# obj.remove(key)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy