Skip to content

Commit fd4fe3b

Browse files
committed
solve problem Design Hashset
1 parent 1ee9782 commit fd4fe3b

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ All solutions will be accepted!
212212
|707|[Design Linked List](https://leetcode-cn.com/problems/design-linked-list/description/)|[java/py/js](./algorithms/DesignLinkedList)|Easy|
213213
|622|[Design Circular Queue](https://leetcode-cn.com/problems/design-circular-queue/description/)|[java/py/js](./algorithms/DesignCircularQueue)|Easy|
214214
|641|[Design Circular Deque](https://leetcode-cn.com/problems/design-circular-deque/description/)|[java/py/js](./algorithms/DesignCircularDeque)|Easy|
215+
|705|[Design Hashset](https://leetcode-cn.com/problems/design-hashset/description/)|[java/py/js](./algorithms/DesignHashset)|Easy|
215216

216217
# Database
217218
|#|Title|Solution|Difficulty|

algorithms/DesignHashset/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Design Hashset
2+
This problem is easy to solve
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class MyHashSet {
2+
int[] buckets;
3+
4+
/** Initialize your data structure here. */
5+
public MyHashSet() {
6+
buckets = new int[1000000];
7+
}
8+
9+
public void add(int key) {
10+
buckets[key] = 1;
11+
}
12+
13+
public void remove(int key) {
14+
buckets[key] = 0;
15+
}
16+
17+
/** Returns true if this set did not already contain the specified element */
18+
public boolean contains(int key) {
19+
return buckets[key] == 1;
20+
}
21+
}
22+
23+
/**
24+
* Your MyHashSet object will be instantiated and called as such:
25+
* MyHashSet obj = new MyHashSet();
26+
* obj.add(key);
27+
* obj.remove(key);
28+
* boolean param_3 = obj.contains(key);
29+
*/

algorithms/DesignHashset/solution.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Initialize your data structure here.
3+
*/
4+
var MyHashSet = function() {
5+
this.buckets = new Array(1000000)
6+
this.buckets.fill(0)
7+
};
8+
9+
/**
10+
* @param {number} key
11+
* @return {void}
12+
*/
13+
MyHashSet.prototype.add = function(key) {
14+
this.buckets[key] = 1
15+
};
16+
17+
/**
18+
* @param {number} key
19+
* @return {void}
20+
*/
21+
MyHashSet.prototype.remove = function(key) {
22+
this.buckets[key] = 0
23+
};
24+
25+
/**
26+
* Returns true if this set did not already contain the specified element
27+
* @param {number} key
28+
* @return {boolean}
29+
*/
30+
MyHashSet.prototype.contains = function(key) {
31+
return this.buckets[key] === 1
32+
};
33+
34+
/**
35+
* Your MyHashSet object will be instantiated and called as such:
36+
* var obj = Object.create(MyHashSet).createNew()
37+
* obj.add(key)
38+
* obj.remove(key)
39+
* var param_3 = obj.contains(key)
40+
*/

algorithms/DesignHashset/solution.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class MyHashSet(object):
2+
3+
def __init__(self):
4+
"""
5+
Initialize your data structure here.
6+
"""
7+
self.buckets = [0] * 1000000
8+
9+
def add(self, key):
10+
"""
11+
:type key: int
12+
:rtype: void
13+
"""
14+
self.buckets[key] = 1
15+
16+
def remove(self, key):
17+
"""
18+
:type key: int
19+
:rtype: void
20+
"""
21+
self.buckets[key] = 0
22+
23+
def contains(self, key):
24+
"""
25+
Returns true if this set did not already contain the specified element
26+
:type key: int
27+
:rtype: bool
28+
"""
29+
return self.buckets[key] == 1
30+
31+
32+
# Your MyHashSet object will be instantiated and called as such:
33+
# obj = MyHashSet()
34+
# obj.add(key)
35+
# obj.remove(key)
36+
# param_3 = obj.contains(key)

0 commit comments

Comments
 (0)