-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLc705.java
More file actions
50 lines (44 loc) · 1.12 KB
/
Lc705.java
File metadata and controls
50 lines (44 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package leetcode;
import java.util.ArrayList;
import java.util.Arrays;
/**
* @author Kuma
* @date 2021年3月13日
* 705. 设计哈希集合
*/
public class Lc705 {
public static void main(String[] args) {
MyHashSet h = new MyHashSet();
h.add(1);
h.add(2);
h.contains(1);
h.remove(1);
}
}
class MyHashSet {
ArrayList<ArrayList<Integer>> hash;
int max = 10;
/** Initialize your data structure here. */
public MyHashSet() {
hash = new ArrayList<>();
for (int i = 0; i < max; i++) {
hash.add(new ArrayList<>());
}
}
public void add(int key) {
int hashCode = key % max;
if (!hash.get(hashCode).contains(key)){
hash.get(hashCode).add(key);
}
}
public void remove(int key) {
int hashCode = key % max;
if (hash.get(hashCode).contains(key)){
hash.get(hashCode).remove((Integer)key);
}
}
/** Returns true if this set contains the specified element */
public boolean contains(int key) {
return hash.get(key % max).contains(key);
}
}