当前位置:网站首页>Leetcode 705. Design hash collection
Leetcode 705. Design hash collection
2022-06-28 14:57:00 【I'm not xiaohaiwa~~~~】

Design a hash set without using any built-in hash table Library (HashSet).
Realization MyHashSet class :
- void add(key) Insert a value into the hash collection key .
- bool contains(key) Returns whether the value exists in the hash set key .
- void remove(key) Set the given value key Remove... From the hash set . If there is no such value in the hash set , Do nothing .
Example :
Input :
["MyHashSet", "add", "add", "contains", "contains", "add", "contains", "remove", "contains"]
[[], [1], [2], [1], [3], [2], [2], [2], [2]]
Output :
[null, null, null, true, false, null, true, null, false]
explain :
MyHashSet myHashSet = new MyHashSet();
myHashSet.add(1); // set = [1]
myHashSet.add(2); // set = [1, 2]
myHashSet.contains(1); // return True
myHashSet.contains(3); // return False ,( Not found )
myHashSet.add(2); // set = [1, 2]
myHashSet.contains(2); // return True
myHashSet.remove(2); // set = [1]
myHashSet.contains(2); // return False ,( Removed )
Tips :
- 0 <= key <= 10^6
- Call at most 10^4 Time add、remove and contains
Code:
class MyHashSet {
public:
MyHashSet() {
}
void add(int key) {
int cnt=count(vec.begin(),vec.end(),key);
if(!cnt)
vec.push_back(key);
}
void remove(int key) {
vector<int>::iterator it=find(vec.begin(),vec.end(),key);
if(it!=vec.end())
{
vec[it-vec.begin()]=-1;
}
}
bool contains(int key) {
return count(vec.begin(),vec.end(),key)!=0;
}
private:
vector<int>vec;
};
/** * Your MyHashSet object will be instantiated and called as such: * MyHashSet* obj = new MyHashSet(); * obj->add(key); * obj->remove(key); * bool param_3 = obj->contains(key); */
边栏推荐
- 美国乔布斯,殁了;中国乔布斯,卖了
- Technical trendsetter
- 字节跳动埋点数据流建设与治理实践
- What are the consequences of opening an account with Huatai Securities? How to open an account is the safest
- 叮!Techo Day 腾讯技术开放日如约而至!
- The latest pycharm activation cracking code in 2022 is permanent_ Detailed installation tutorial (applicable to multiple versions)
- 腾讯再遭大股东Prosus减持:后者还从京东套现37亿美元
- 从莫高窟到太平洋,海量数据找到了新家园
- 老板囑咐了三遍:低調、低調、低調
- Flutter series: offstage in flutter
猜你喜欢

Recommendation letter brain correspondent: if love is just a chemical reaction, can you still believe in love?

抽奖动画 - 鲤鱼跳龙门

完整的模型训练套路(一)

Introduction to common components of IOT low code platform

Kwai investment e-commerce service provider Yixin optimization

Recommended practice sharing of Zhilian recruitment based on Nebula graph
![[C language] nextday problem](/img/7b/422792e07dd321e3a37c1fff55c0ca.png)
[C language] nextday problem

量子前沿英雄谱|“光量子探险家”McMahon:将任何物理系统变成神经网络

With 120billion yuan, she will ring the bell for IPO again

从小小线虫谈起——溯源神经系统进化,开启生命模拟
随机推荐
2022 recurrent training question bank and online simulation examination for safety inspection of metal and nonmetal mines (underground mines)
Is PMP really useful?
Angers medical sprint scientific innovation board: annual revenue of RMB 300million and proposed fund raising of RMB 770million
Spacetutorial (continuous updating...)
Q-tester 3.2: applicable to development, production and after-sales diagnostic test software
物联网低代码平台常用《组件介绍》
Leetcode (665) -- non decreasing column
JS judge whether the string is empty or not
Could you tell me whether the batch addition of Oracle such as insert all was not blocked?
名创优品通过上市聆讯:寻求双重主要上市 年营收91亿
华为能成“口红一哥”,或者“带货女王”吗?
[C language] how to generate normal or Gaussian random numbers
After QQ was stolen, a large number of users "died"
第四大运营商,难成「鲶鱼」
币圈大地震:去年赚100万,今年亏500万
证券公司和银行哪个更安全 怎么办理开户最安全
蔚来潜藏的危机:过去、现在到未来
2022 operation of simulation test platform for 100 simulated questions of main principals of metal and nonmetal mines (underground mines)
With 120billion yuan, she will ring the bell for IPO again
Send2vec tutorial