LeetCode 705. Design HashSet (设计哈希集合)

题目标签:HashMap

  题目让我们设计一个 hashset,有add,contains,remove 功能。

  建立一个boolean array,index 是数字的值,具体看code。

 

Java Solution:

Runtime: 58 ms, faster than 90.21% 

Memory Usage: 56.3 MB, less than 68.53%

完成日期:03/18/2019

关键点:boolean array

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

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

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

posted @ 2019-05-19 04:50  Jimmy_Cheng  阅读(447)  评论(0编辑  收藏  举报