-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy path705DesignHashSet.js
More file actions
62 lines (56 loc) · 1.37 KB
/
705DesignHashSet.js
File metadata and controls
62 lines (56 loc) · 1.37 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
57
58
59
60
61
62
// Time Complexity : O(1)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No
var MyHashSet = function () {
this.size = 1009;
this.buckets = new Array(this.size);
for (let i = 0; i < this.size; i++) {
this.buckets[i] = [];
}
};
/**
* @param {number} key
* @return {void}
*/
MyHashSet.prototype.hash1 = function (key) {
return key % this.size;
};
MyHashSet.prototype.hash2 = function (key) {
return Math.floor(key / this.size);
};
MyHashSet.prototype.add = function (key) {
const h1 = this.hash1(key);
const h2 = this.hash2(key);
// if(!this.buckets[h1])
//this.buckets[h1] = new Array(993).fill(false)
this.buckets[h1][h2] = true;
};
/**
* @param {number} key
* @return {void}
*/
MyHashSet.prototype.remove = function (key) {
const h1 = this.hash1(key);
const h2 = this.hash2(key);
if (this.buckets[h1]) this.buckets[h1][h2] = false;
};
/**
* @param {number} key
* @return {boolean}
*/
MyHashSet.prototype.contains = function (key) {
const h1 = this.hash1(key);
const h2 = this.hash2(key);
if (this.buckets[h1])
if (this.buckets[h1][h2]) return true;
else return false;
else return false;
};
/**
* Your MyHashSet object will be instantiated and called as such:
* var obj = new MyHashSet()
* obj.add(key)
* obj.remove(key)
* var param_3 = obj.contains(key)
*/