Skip to content

Commit 0838851

Browse files
add 706
1 parent a83b5cd commit 0838851

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Your ideas/fixes/algorithms are more than welcome!
112112
|713|[Subarray Product Less Than K](https://leetcode.com/problems/subarray-product-less-than-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_713.java) | O(n) | O(1) | |Medium |
113113
|712|[Minimum ASCII Delete Sum for Two Strings](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_712.java) | O(m*n) | O(m*n) | |Medium | DP
114114
|709|[To Lower Case](https://leetcode.com/problems/to-lower-case/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_709.java) | O(n) | O(1) | |Easy| String
115+
|706|[Design HashMap](https://leetcode.com/problems/design-hashmap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_706.java) | O(n) | O(n) | |Easy| Design
115116
|705|[Design HashSet](https://leetcode.com/problems/design-hashset/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_705.java) | O(1) | O(n) | |Easy| Design
116117
|704|[Binary Search](https://leetcode.com/problems/binary-search/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_704.java) | O(logn) | O(1) | |Easy| Binary Search
117118
|700|[Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_700.java) | O(n) | O(h) | |Easy| recusion, dfs
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.fishercoder.solutions;
2+
3+
/**706. Design HashMap
4+
*
5+
* Design a HashMap without using any built-in hash table libraries.
6+
*
7+
* To be specific, your design should include these functions:
8+
*
9+
* put(key, value) : Insert a (key, value) pair into the HashMap. If the value already exists in the HashMap, update the value.
10+
* get(key): Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
11+
* remove(key) : Remove the mapping for the value key if this map contains the mapping for the key.
12+
*
13+
* Example:
14+
*
15+
* MyHashMap hashMap = new MyHashMap();
16+
* hashMap.put(1, 1);
17+
* hashMap.put(2, 2);
18+
* hashMap.get(1); // returns 1
19+
* hashMap.get(3); // returns -1 (not found)
20+
* hashMap.put(2, 1); // update the existing value
21+
* hashMap.get(2); // returns 1
22+
* hashMap.remove(2); // remove the mapping for 2
23+
* hashMap.get(2); // returns -1 (not found)
24+
*
25+
* Note:
26+
*
27+
* All keys and values will be in the range of [0, 1000000].
28+
* The number of operations will be in the range of [1, 10000].
29+
* Please do not use the built-in HashMap library.*/
30+
public class _706 {
31+
public static class Solution1 {
32+
/**
33+
* credit: https://leetcode.com/problems/design-hashmap/discuss/152746/Java-Solution
34+
*/
35+
class MyHashMap {
36+
37+
final ListNode[] nodes = new ListNode[10000];
38+
39+
public void put(int key, int value) {
40+
int i = idx(key);
41+
if (nodes[i] == null) {
42+
nodes[i] = new ListNode(-1, -1);
43+
}
44+
ListNode prev = find(nodes[i], key);
45+
if (prev.next == null) {
46+
prev.next = new ListNode(key, value);
47+
} else {
48+
prev.next.val = value;
49+
}
50+
}
51+
52+
public int get(int key) {
53+
int i = idx(key);
54+
if (nodes[i] == null) {
55+
return -1;
56+
}
57+
ListNode node = find(nodes[i], key);
58+
return node.next == null ? -1 : node.next.val;
59+
}
60+
61+
public void remove(int key) {
62+
int i = idx(key);
63+
if (nodes[i] == null) {
64+
return;
65+
}
66+
ListNode prev = find(nodes[i], key);
67+
if (prev.next == null) {
68+
return;
69+
}
70+
prev.next = prev.next.next;
71+
}
72+
73+
int idx(int key) {
74+
return Integer.hashCode(key) % nodes.length;
75+
}
76+
77+
ListNode find(ListNode bucket, int key) {
78+
ListNode node = bucket, prev = null;
79+
while (node != null && node.key != key) {
80+
prev = node;
81+
node = node.next;
82+
}
83+
return prev;
84+
}
85+
86+
class ListNode {
87+
int key, val;
88+
ListNode next;
89+
90+
ListNode(int key, int val) {
91+
this.key = key;
92+
this.val = val;
93+
}
94+
}
95+
}
96+
97+
/**
98+
* Your MyHashMap object will be instantiated and called as such:
99+
* MyHashMap obj = new MyHashMap();
100+
* obj.put(key,value);
101+
* int param_2 = obj.get(key);
102+
* obj.remove(key);
103+
*/
104+
}
105+
}

0 commit comments

Comments
 (0)