设计hash表

class MyHashSet {
    private:
        vector<list<int>> data;
        static const int base = 769;
        static int hash(int key){
            return key % base;
        }
public:
    MyHashSet(): data(base) {}
    
    void add(int key) {
        int index = hash(key);
        for(auto it = data[index].begin(); it != data[index].end(); it++){
            if(*it == key){
                return ;
            }
        }
        data[index].push_back(key);
    }
    
    void remove(int key) {
        int index = hash(key);
        for(auto it = data[index].begin(); it != data[index].end(); it++){
            if(*it == key){
                data[index].erase(it);
                return;
            }
        }
    }
    
    bool contains(int key) {
        int index = hash(key);
        for(auto it = data[index].begin(); it != data[index].end(); it++){
            if(*it == key){
                return true;
            }
        }
        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);
 */
class MyHashSet {
    private List<List<Integer>> data;
    static final int base = 769;
    static int hash(int key){
        return key % base;
    }
    public MyHashSet() {
        this.data = new ArrayList<>(base);
        for(int i = 0; i < base; i++){
            data.add(new ArrayList<>()); 
            //new ArrayList<>(base) 创建一个初始容量为 base 的
            // ArrayList,但需要注意的是,这并不会预先填充 base 个 List<Integer> 对象。
            //你需要手动初始化每个元素。
        }
    }
    
    public void add(int key) {
        int index = hash(key);
        List<Integer> list = data.get(index);
        if(list.contains(key)){
            return;
        }
        list.add(key);
    }
    
    public void remove(int key) {
        int index = hash(key);
        List<Integer> list = data.get(index);
        if(!list.contains(key)){
            return;
        }    
        list.remove(Integer.valueOf(key));
    }
    
    public boolean contains(int key) {
        int index = hash(key);
        List<Integer> list = data.get(index);
        if(!list.contains(key)){
            return false;
        } 
        return true;
    }
}

/**
 * Your MyHashSet object will be instantiated and called as such:
 * MyHashSet obj = new MyHashSet();
 * obj.add(key);
 * obj.remove(key);
 * boolean param_3 = obj.contains(key);
 */
class MyHashSet {
    private static final int BASE = 769;
    private LinkedList[] data;

    /** Initialize your data structure here. */
    public MyHashSet() {
        data = new LinkedList[BASE];
        for (int i = 0; i < BASE; ++i) {
            data[i] = new LinkedList<Integer>();
        }
    }
    
    public void add(int key) {
        int h = hash(key);
        Iterator<Integer> iterator = data[h].iterator();
        while (iterator.hasNext()) {
            Integer element = iterator.next();
            if (element == key) {
                return;
            }
        }
        data[h].offerLast(key);
    }
    
    public void remove(int key) {
        int h = hash(key);
        Iterator<Integer> iterator = data[h].iterator();
        while (iterator.hasNext()) {
            Integer element = iterator.next();
            if (element == key) {
                data[h].remove(element);
                return;
            }
        }
    }
    
    /** Returns true if this set contains the specified element */
    public boolean contains(int key) {
        int h = hash(key);
        Iterator<Integer> iterator = data[h].iterator();
        while (iterator.hasNext()) {
            Integer element = iterator.next();
            if (element == key) {
                return true;
            }
        }
        return false;
    }

    private static int hash(int key) {
        return key % BASE;
    }
}

class MyHashSet(object):
    
    def __init__(self):
        self.base = 769
        self.data = [[] for _ in range(self.base)]

    def hash(self, key):
        return key % self.base

    def add(self, key):
        index = self.hash(key)
        list = self.data[index]
        if key not in list:
            list.append(key)
        return
        
    def remove(self, key):
        index = self.hash(key)
        list = self.data[index]
        if key not in list:
           return
        list.remove(key)

    def contains(self, key):
        index = self.hash(key)
        list = self.data[index]
        if key in list:
            return True
        return False
        


# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)
posted on 2025-02-18 11:46  爱吐水的小火龙  阅读(6)  评论(0)    收藏  举报