forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSample.java
More file actions
56 lines (46 loc) · 1.65 KB
/
Sample.java
File metadata and controls
56 lines (46 loc) · 1.65 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
51
52
53
54
55
56
// Time Complexity : O(1)
// Space Complexity :
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this :
// Your code here along with comments explaining your approach
/**
* 1. Creating HashSet and use double hashing for resolving collisions.
* 2. Use 2D square matrix/array for storing values.
* 3. Primary array will store reference of secondary array.
* 4. Secondary array will store actual values.
* 5. For first hash function we will use Modulo(%) with primary array Size.
* 6. For second hash function we will use division(/) with primary array Size.
* 7. For 0 index in primary function we have to create a secondary array with (bucket size + 1).
*/
class MyHashSet {
private int bucketSize = 1000;
private boolean[][] buckets = new boolean[bucketSize][];
private int hash1(int key){
return key % bucketSize;
}
private int hash2(int key){
return key/bucketSize;
}
public MyHashSet() {
}
// Time Complexity : O(1)
public void add(int key) {
if(buckets[hash1(key)]== null){
if(hash1(key) == 0)
buckets[hash1(key)] = new boolean[bucketSize + 1];
else
buckets[hash1(key)] = new boolean[bucketSize];
}
buckets[hash1(key)][hash2(key)] = true;
}
// Time Complexity : O(1)
public void remove(int key) {
if(buckets[hash1(key)]== null) return;
buckets[hash1(key)][hash2(key)] = false;
}
// Time Complexity : O(1)
public boolean contains(int key) {
if(buckets[hash1(key)]== null) return false;
return buckets[hash1(key)][hash2(key)];
}
}