Java/705. Design HashSet 设计哈希集合

题目

Java/705. Design HashSet 设计哈希集合


Java/705. Design HashSet 设计哈希集合

 

 

 

代码部分一(150ms 48.40%)

class MyHashSet {

    /** Initialize your data structure here. */
    int[] map = new int[1000005];
    public MyHashSet() {
        
    }
    
    public void add(int key) {
        map[key] = 1;
    }
    
    public void remove(int key) {
        map[key] = 0;
    }
    
    /** Returns true if this set did not already contain the specified element */
    public boolean contains(int key) {
        if(map[key] == 1) return true;
        
        return false;
    }
}

 

代码部分二(106ms 94.06%)

class MyHashSet {

    /** Initialize your data structure here. */
    boolean[] map = new boolean[1000005];
    public MyHashSet() {
        
    }
    
    public void add(int key) {
        map[key] = true;
    }
    
    public void remove(int key) {
        map[key] = false;
    }
    
    /** Returns true if this set did not already contain the specified element */
    public boolean contains(int key) {
        return map[key] == true;
    }
}