Likou 705. Design hash collection hash (chain address method) bit operation to achieve hash

https://leetcode-cn.com/problems/design-hashset/
Insert picture description here
Idea 1: Unpretentious and boring chain address method, through vector, forward _ list vector, forward\_listv e c t o r , f o r w a r d _ l i s t (the latter is a singly linked list).

class MyHashSet {
    
    
public:
    /** Initialize your data structure here. */
    const int num=2333;
    vector<forward_list<int>> hashTable;
    MyHashSet() {
    
    
        hashTable.resize(num);
    }
    
    void add(int key) {
    
    
        if(!contains(key))
            hashTable[key%num].push_front(key);
    }
    
    void remove(int key) {
    
    
        int idx=key%num;
        forward_list<int>::iterator it=hashTable[idx].before_begin();
        forward_list<int>::iterator nxt=hashTable[idx].begin();
        forward_list<int>::iterator ed=hashTable[idx].end();
        while(nxt!=ed&&*nxt!=key)
            it=nxt++;
        if(nxt!=ed)
            hashTable[idx].erase_after(it);
    }
    
    /** Returns true if this set contains the specified element */
    bool contains(int key) {
    
    
        int idx=key%num;
        forward_list<int>::iterator it=hashTable[idx].begin();
        forward_list<int>::iterator ed=hashTable[idx].end();
        while(it!=ed&&*it!=key)
            ++it;
        return it!=ed;
    }
};

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

Idea 2: Bitwise arithmetic implements hashing. This idea is quite clever. Consider using an unsigned long long unsigned\ long\ longu n s i g n e d l o n g l o n g   array realizes hashing, might as well set the modulus asbase baseb a s e , then for anykey keyk e y , we can convert it into two parts:⌊ key / base ⌋ \lfloor key/base \rfloorkey/base k e y % b a s e key\%base k e y % b a s e , the first part determines its subscript in the array, and the second part determines the binary representation of its value at that place (the code should be more intuitive here). Consideringull ullThe value range of u l l type is[0, 2 64 − 1] [0,2^{64}-1][0,2641 ] , aull ullu l l type corresponds to up to 64 values, sobase = 64 base=64base=6 4 , the range of the array should be⌊ max _ key / base ⌋ + 1 \lfloor max\_key/base \rfloor+1max_key/base+1

class MyHashSet {
    
    
public:
    /** Initialize your data structure here. */
    const int base=64;
    vector<unsigned long long> hashTable;
    MyHashSet() {
    
    
        int max=1e6,num=max/base;
        if(num*base==max)
            ++num;
        hashTable.resize(num);
    }
    
    void add(int key) {
    
    
        hashTable[key/base]|=1ull<<(key%base);
    }
    
    void remove(int key) {
    
    
        hashTable[key/base]&=~(1ull<<(key%base));
    }
    
    /** Returns true if this set contains the specified element */
    bool contains(int key) {
    
    
        return hashTable[key/base]&(1ull<<(key%base));
    }
};

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

Guess you like

Origin blog.csdn.net/xiji333/article/details/114718907