Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite5af0c1

Browse files
add one more solution for 706
1 parent37c743b commite5af0c1

File tree

2 files changed

+118
-7
lines changed

2 files changed

+118
-7
lines changed

‎src/main/java/com/fishercoder/solutions/_706.java

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,99 @@ class ListNode {
6868
}
6969
}
7070
}
71+
}
72+
73+
publicstaticclassSolution2 {
74+
75+
publicstaticclassMyHashMap {
76+
/**
77+
* Considering the given constraints for this problem on LeetCode, load factors and resizing/rehashing are not considered. Thus an EASY problem.
78+
* <p>
79+
* inspired by: https://leetcode.com/problems/design-hashmap/discuss/225312/hashmaparraylinkedlistcollision
80+
*/
81+
classNode {
82+
/**
83+
* We need to have both key and val in this ListNode because all values input key are hashed to the same bucket, so we need its original key
84+
* to be a differentiator within this bucket.
85+
*/
86+
intval;
87+
intkey;
88+
Nodenext;
89+
90+
publicNode(intkey,intval) {
91+
this.key =key;
92+
this.val =val;
93+
}
94+
}
95+
96+
Node[]nodes;
97+
intSIZE =1000000;
98+
99+
/**
100+
* Initialize your data structure here.
101+
*/
102+
publicMyHashMap() {
103+
nodes =newNode[SIZE];
104+
}
105+
106+
/**
107+
* value will always be non-negative.
108+
*/
109+
publicvoidput(intkey,intvalue) {
110+
intindex =getHashedKey(key);
111+
Nodehead =nodes[index];
112+
Nodetmp =head;
113+
while (tmp !=null) {
114+
if (tmp.key ==key) {
115+
tmp.val =value;
116+
return;
117+
}
118+
tmp =tmp.next;
119+
}
120+
NodenewHead =newNode(key,value);
121+
newHead.next =head;
122+
nodes[index] =newHead;
123+
}
124+
125+
privateintgetHashedKey(intkey) {
126+
returnInteger.hashCode(key) %SIZE;
127+
}
71128

72-
/**
73-
* Your MyHashMap object will be instantiated and called as such:
74-
* MyHashMap obj = new MyHashMap();
75-
* obj.put(key,value);
76-
* int param_2 = obj.get(key);
77-
* obj.remove(key);
78-
*/
129+
/**
130+
* Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key
131+
*/
132+
publicintget(intkey) {
133+
Nodehead =nodes[getHashedKey(key)];
134+
Nodetmp =head;
135+
while (tmp !=null &&tmp.key !=key) {
136+
tmp =tmp.next;
137+
}
138+
if (tmp ==null) {
139+
return -1;
140+
}
141+
if (tmp.key ==key) {
142+
returntmp.val;
143+
}
144+
return -1;
145+
}
146+
147+
/**
148+
* Removes the mapping of the specified value key if this map contains a mapping for the key
149+
*/
150+
publicvoidremove(intkey) {
151+
/**We can just set the value of this key to -1 since the constraint for this problem is that all values are >= 0*/
152+
Nodenode =nodes[getHashedKey(key)];
153+
Nodetmp =node;
154+
Nodepre =newNode(-1, -1);
155+
pre.next =tmp;
156+
while (tmp !=null) {
157+
if (tmp.key ==key) {
158+
tmp.val = -1;
159+
return;
160+
}
161+
tmp =tmp.next;
162+
}
163+
}
164+
}
79165
}
80166
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._706;
4+
importorg.junit.Test;
5+
6+
importstaticorg.junit.Assert.assertEquals;
7+
8+
publicclass_706Test {
9+
privatestatic_706.Solution2.MyHashMapmyHashMap;
10+
11+
@Test
12+
publicvoidtest1() {
13+
myHashMap =new_706.Solution2.MyHashMap();
14+
15+
myHashMap.put(1,1);
16+
myHashMap.put(2,2);
17+
assertEquals(1,myHashMap.get(1));
18+
assertEquals(-1,myHashMap.get(3));
19+
myHashMap.put(2,1);
20+
assertEquals(1,myHashMap.get(2));
21+
myHashMap.remove(2);
22+
assertEquals(-1,myHashMap.get(2));
23+
}
24+
25+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp