输入:
要求:实现add、remove、contains函数
输出:
思路:偷懒了,用一个vector<int>当做容器处理。
add:直接push_back
remove:遍历vector,找到就置为-1
contains:遍历查找
虽然用vector慢归慢,但是功能是都实现了(bushi
复杂度:
时间复杂度:add O(1) remove O(n) contains O(n)
空间复杂度:O(n)
class MyHashSet {
private:
vector<int> tmp;
public:
MyHashSet() {
}
void add(int key) {
tmp.push_back(key);
}
void remove(int key) {
for (int& t:tmp) {
if (t == key) {
t = -1;
}
}
return;
}
bool contains(int key) {
for (int t:tmp) {
if (t == key) {
return true;
}
}
return false;
}
};
1615

被折叠的 条评论
为什么被折叠?



