-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashSet.java
More file actions
40 lines (35 loc) · 1.19 KB
/
HashSet.java
File metadata and controls
40 lines (35 loc) · 1.19 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
// https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/549/week-1-august-1st-august-7th/3410/
class MyHashSet {
int hashsize = 10000;
List<Integer>[] hash;
/** Initialize your data structure here. */
public MyHashSet() {
hash = new LinkedList[hashsize];
}
int function(int key){
return key%hashsize;
}
public void add(int key) {
int temp = function(key),f=0;
if(hash[temp] == null) hash[temp] = new LinkedList<>();
if(hash[temp].indexOf(key) == -1) hash[temp].add(key);
}
public void remove(int key) {
int temp = function(key),f=0;
if(hash[temp] == null) return ;
if(hash[temp].indexOf(key) != -1) hash[temp].remove(hash[temp].indexOf(key));
}
/** Returns true if this set contains the specified element */
public boolean contains(int key) {
int temp = function(key),f=0;
if(hash[temp] == null || hash[temp].indexOf(key) == -1) return false;
return true;
}
}
/**
* Your MyHashSet object will be instantiated and called as such:
* MyHashSet obj = new MyHashSet();
* obj.add(key);
* obj.remove(key);
* boolean param_3 = obj.contains(key);
*/