Files
leetcode-master/problems/0705.设计哈希集合.md
youngyangyang04 badde5a622 Update
2020-08-01 16:49:18 +08:00

850 B
Raw Blame History

题目地址

https://leetcode-cn.com/problems/design-hashset/

思路

使用数组便可以实现这个哈希集合

代码

class MyHashSet {

public:
    vector<int> hashTable;
    /** Initialize your data structure here. */
    MyHashSet() {
        vector<int> table(1000001, 0);
        hashTable = table;
    }

    void add(int key) {
        hashTable[key] = 1;
    }

    void remove(int key) {
        hashTable[key] = 0;
    }

    /** Returns true if this set contains the specified element */
    bool contains(int key) {
        return hashTable[key];
    }
};

更多算法干货文章持续更新可以微信搜索「代码随想录」第一时间围观关注后回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等就可以获得我多年整理的学习资料。