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/

原文地址:https://www.cnblogs.com/jimmycheng/p/10888009.html

时间: 2024-10-10 05:38:47

LeetCode 705. Design HashSet (设计哈希集合)的相关文章

LeetCode 706. Design HashMap (设计哈希映射)

题目标签:HashMap 题目让我们设计一个 hashmap, 有put, get, remove 功能. 建立一个 int array, index 是key, 值是 value,具体看code. Java Solution: Runtime: 76 ms, faster than 27.53% Memory Usage: 58.2 MB, less than 31.57% 完成日期:03/18/2019 关键点:int array class MyHashMap { int [] map;

705.设计哈希集合

2020-04-07 设计哈希集合 不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 add(value):向哈希集合中插入一个值. contains(value) :返回哈希集合中是否存在这个值. remove(value):将给定值从哈希集合中删除.如果哈希集合中没有这个值,什么也不做. 题解: 思路1: 使用数组设计哈希集合 (不建议) 之所以不建议是因为性能差 每次add,contains或者delete都要遍历看看这个值是不是存在 // 使用数组做hash

[Swift]LeetCode705. 设计哈希集合 | Design HashSet

Design a HashSet without using any built-in hash table libraries. To be specific, your design should include these functions: add(value): Insert a value into the HashSet. contains(value) : Return whether the value exists in the HashSet or not. remove

[LeetCode] Design HashSet 设计HashSet

Design a HashSet without using any built-in hash table libraries. To be specific, your design should include these functions: add(value): Insert a value into the HashSet. contains(value) : Return whether the value exists in the HashSet or not. remove

[LeetCode&Python] Problem 705. Design HashSet

Design a HashSet without using any built-in hash table libraries. To be specific, your design should include these functions: add(value): Insert a value into the HashSet. contains(value) : Return whether the value exists in the HashSet or not. remove

[LeetCode] 355. Design Twitter 设计推特

Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user's news feed. Your design should support the following methods: postTweet(userId, tweetId): Compo

[LeetCode] 534. Design TinyURL 设计短网址

Note: For the coding companion problem, please see: Encode and Decode TinyURL. How would you design a URL shortening service that is similar to TinyURL? Background:TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com

705.Design HashSet

Design a HashSet without using any built-in hash table libraries. To be specific, your design should include these functions: add(value): Insert a value into the HashSet. contains(value) : Return whether the value exists in the HashSet or not. remove

706.设计哈希映射

2020-04-08 设计哈希映射 不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get(key):返回给定的键所对应的值,如果映射中不包含这个键,返回-1. remove(key):如果映射中存在这个键,删除这个数值对. 题解: 思路1: 使用对象设计哈希集合 /** * Initialize your data structure here. */ va