当前位置:网站首页>Leetcode 705. 设计哈希集合
Leetcode 705. 设计哈希集合
2022-06-28 11:33:00 【我不是萧海哇~~~~】
不使用任何内建的哈希表库设计一个哈希集合(HashSet)。
实现 MyHashSet 类:
- void add(key) 向哈希集合中插入值 key 。
- bool contains(key) 返回哈希集合中是否存在这个值 key 。
- void remove(key) 将给定值 key 从哈希集合中删除。如果哈希集合中没有这个值,什么也不做。
示例:
输入:
["MyHashSet", "add", "add", "contains", "contains", "add", "contains", "remove", "contains"]
[[], [1], [2], [1], [3], [2], [2], [2], [2]]
输出:
[null, null, null, true, false, null, true, null, false]
解释:
MyHashSet myHashSet = new MyHashSet();
myHashSet.add(1); // set = [1]
myHashSet.add(2); // set = [1, 2]
myHashSet.contains(1); // 返回 True
myHashSet.contains(3); // 返回 False ,(未找到)
myHashSet.add(2); // set = [1, 2]
myHashSet.contains(2); // 返回 True
myHashSet.remove(2); // set = [1]
myHashSet.contains(2); // 返回 False ,(已移除)
提示:
- 0 <= key <= 10^6
- 最多调用 10^4 次 add、remove 和 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); */
边栏推荐
- Remote login sshd service
- 携手Cigent:群联为SSD主控固件引入高级网络安全防护特性
- Oracle date format exception: invalid number
- MySQL cannot query the maximum value using the max function
- 面试步骤的面试技巧
- day33 js笔记 事件(下)2021.09.28
- Still using simpledateformat for time formatting? Be careful that the project collapses!
- 董宇辉,新东方以及凤凰卫视
- What is the main chain system?
- 无法重新声明块范围变量
猜你喜欢
智联招聘基于 Nebula Graph 的推荐实践分享
Day31 JS notes DOM 2021.09.26
水果FL Studio/Cubase/Studio one音乐宿主软件对比
day29 js笔记 2021.09.23
Using soapUI to obtain freemaker's FTL file template
Is it feasible to be a programmer at the age of 26?
Prepare for Jin San Yin Si I. testers without experience in automated testing projects should look at it quickly
无法重新声明块范围变量
Redis 原理 - List
Day33 JS note event (Part 2) September 28, 2021
随机推荐
建立自己的网站(18)
Intranet penetration in the working group environment: some basic methods
The development and principle of the metacosmic system
水果FL Studio/Cubase/Studio one音乐宿主软件对比
Recommended practice sharing of Zhilian recruitment based on Nebula graph
Day30 JS notes BOM and DOM 2021.09.24
day28 严格模式、字符串 js 2021.09.22
day33 js笔记 事件(下)2021.09.28
赛尔号抽奖模拟求期望
setInterval、setTimeout和requestAnimationFrame
Come on, yuanuniverse. Sure enough, the heat won't pass for a while
Batch will png . bmp . JPEG format pictures are converted to Jpg format picture
Use logrotate to automatically cut the website logs of the pagoda
Prepare for Jin San Yin Si I. testers without experience in automated testing projects should look at it quickly
AGCO AI frontier promotion (2.16)
携手Cigent:群联为SSD主控固件引入高级网络安全防护特性
Tidb v6.0.0 (DMR): initial test of cache table - tidb Book rush
Setinterval, setTimeout and requestanimationframe
买股票在中金证券经理的开户二维码上开户安全吗?求大神赐教
6.A-B