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(value): Remove a value in the HashSet. If the value does not exist in the HashSet, do nothing.

Example:

MyHashSet hashSet = new MyHashSet();

hashSet.add(1);

hashSet.add(2);

hashSet.contains(1); // returns true

hashSet.contains(3); // returns false (not found)

hashSet.add(2);

hashSet.contains(2); // returns true

hashSet.remove(2);

hashSet.contains(2); // returns false (already removed)

Note:

  • All values will be in the range of [0, 1000000].
  • The number of operations will be in the range of [1, 10000].
  • Please do not use the built-in HashSet library.
class MyHashSet:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.s = set()

    def add(self, key):
        """
        :type key: int
        :rtype: void
        """
        self.s.add(key)
        return

    def remove(self, key):
        """
        :type key: int
        :rtype: void
        """
        self.s.discard(key)
        return

    def contains(self, key):
        """
        Returns true if this set contains the specified element
        :type key: int
        :rtype: bool
        """
        return key in self.s

# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)

原文地址:https://www.cnblogs.com/bernieloveslife/p/9769314.html

时间: 2024-09-29 13:52:02

705.Design HashSet的相关文章

[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 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 [] se

[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

[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

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(共38题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure design [173]Binary Search Tree Iterator [208]Implement Trie (Prefix Tree) [211]Add and Search Word - Data structure desig

【LeetCode】哈希表 hash_table(共88题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target . 题解:我这次最大范围的优化代码, hash-table + one pass,时间复杂度 O(N),空间复杂度 O(N).重点在于动态找,一边生成hash-tabl

170. Two Sum III - Data structure design

Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure.find - Find if there exists any pair of numbers which sum is equal to the value. For example, add(1); ad

【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):