Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit471b1bf

Browse files
authored
Created design_hashset
1 parent7eab2e3 commit471b1bf

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#Design Hashset
2+
###Problem link:[Click me](https://leetcode.com/problems/design-hashset/)
3+
---
4+
5+
-##Question:
6+
>Design a HashSet without using any built-in hash table libraries.
7+
>>Implement`MyHashSet` class:
8+
-`void add(key)` Inserts the value key into the HashSet
9+
-`bool contains(key)` Returns whether the value`key` exists in the HashSet or not.
10+
-`void remove(key)` Removes the value key in the HashSet. If key does not exist in the HashSet, do nothing.
11+
12+
>Example:
13+
14+
Input
15+
["MyHashSet", "add", "add", "contains", "contains", "add", "contains", "remove", "contains"]
16+
[[], [1], [2], [1], [3], [2], [2], [2], [2]]
17+
Output
18+
[null, null, null, true, false, null, true, null, false]
19+
20+
Explanation
21+
MyHashSet myHashSet = new MyHashSet();
22+
myHashSet.add(1); // set = [1]
23+
myHashSet.add(2); // set = [1, 2]
24+
myHashSet.contains(1); // return True
25+
myHashSet.contains(3); // return False, (not found)
26+
myHashSet.add(2); // set = [1, 2]
27+
myHashSet.contains(2); // return True
28+
myHashSet.remove(2); // set = [1]
29+
myHashSet.contains(2); // return False, (already removed)
30+
31+
-##Solution:
32+
**Code :**
33+
34+
```cpp
35+
classMyHashSet {
36+
public:
37+
/** Initialize your data structure here.*/
38+
MyHashSet() : set(1000001) {}
39+
40+
void add(int key) {
41+
set[key] = true;
42+
}
43+
44+
void remove(int key) {
45+
set[key] = false;
46+
}
47+
48+
/** Returns true if this set contains the specified element*/
49+
bool contains(int key) {
50+
return set[key];
51+
}
52+
53+
private:
54+
vector<bool> set;
55+
};
56+
```
57+
58+
---
59+
Reference : [walkccc.me](https://walkccc.me/LeetCode/)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp