天天看點

leetcode刷題 705 設計哈希集合 (C++)

class MyHashSet {
private:
    vector<bool> hashset;
public:
    /** Initialize your data structure here. */
    MyHashSet() {
        hashset = vector<bool> (1000001, false);
    }
    
    void add(int key) {
        hashset[key] = true;
        return;
        
    }
    
    void remove(int key) {
        if(hashset[key])
        {
            hashset[key] = false;
            return;
        }
        return;
        
    }
    
    /** Returns true if this set contains the specified element */
    bool contains(int key) {
        if(hashset[key])
        {
            return true;
        }
        else 
            return false;
    }
};

/**
 * 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);
 */
           

繼續閱讀