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(object):

    def __init__(self):
"""
Initialize your data structure here.
"""
self.hashSet=set() def add(self, key):
"""
:type key: int
:rtype: void
"""
self.hashSet.add(key) def remove(self, key):
"""
:type key: int
:rtype: void
"""
if key in self.hashSet:
self.hashSet.remove(key) def contains(self, key):
"""
Returns true if this set contains the specified element
:type key: int
:rtype: bool
"""
if key in self.hashSet:
return True
else:
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)

  

[LeetCode&Python] Problem 705. Design HashSet的更多相关文章

  1. [LeetCode&Python] Problem 706. Design HashMap

    Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...

  2. 【Leetcode_easy】705. Design HashSet

    problem 705. Design HashSet 题意: solution1: class MyHashSet { public: /** Initialize your data struct ...

  3. 【LeetCode】705. Design HashSet 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位图法 数组法 日期 题目地址:https://le ...

  4. LeetCode 705 Design HashSet 解题报告

    题目要求 Design a HashSet without using any built-in hash table libraries. To be specific, your design s ...

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

    题目标签:HashMap 题目让我们设计一个 hashset,有add,contains,remove 功能. 建立一个boolean array,index 是数字的值,具体看code. Java ...

  6. [LeetCode&Python] Problem 703. Kth Largest Element in a Stream

    Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...

  7. [LeetCode&Python] Problem 661. Image Smoother

    Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...

  8. [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

  9. [LeetCode&Python] Problem 492. Construct the Rectangle

    For a web developer, it is very important to know how to design a web page's size. So, given a speci ...

随机推荐

  1. vim 文本会在末尾自动添加换行 md5文件和数据只不对应

    在linux系统 vim md5data  # 打开文件 写入 abc 保存 md5sum md5Data  的计算值和openssl代码计算值不一样 原因在于linux文本文件末尾自动添加了换行 解 ...

  2. js代码之编程习惯

    编程习惯: 异常处理 如果你没有使用异常处理的习惯,这可能是因为你并未真正的理解它的作用.当你正确使用异常处理之后,你会发现你的代码最显著的变化就是:少了很多的 if-else 语句 . 虽然在 JS ...

  3. win10打文件预览功能

  4. learning at command AT+CIMI

    AT command AT+CIMI [Purpose]        Learning how to get the International Mobile Subscriber Identity ...

  5. java 一些容易忽视的小点-类和对象

    构造器 通过new关键字调用 构造器虽然有返回值,但是不能定义返回值类型(返回值的类型肯定是本类),不能在构造器里使用return返回某个值. 构造器是有权限的,也就是可以添加public,也可以添加 ...

  6. java获得当前系统时间三种方法

    参见: http://blog.csdn.net/cloume/article/details/46624637

  7. OOP⑻

    1.接口: 类 和 对象 对象 is a 类 例子: 小鸟 is a 动物 飞机 is a 交通工具 子弹 is a 武器 卫星 is a 通讯工具 问题? 01. 小鸟 飞机 子弹 卫星 虽然不是一 ...

  8. Saiku通过iframe嵌入web页面(六)

    Saiku通过iframe嵌入系统页面 前提: Saiku已安装好,并且配置了数据源,熟练了saiku的基本使用. 一.将整个Saiku嵌入页面 在web项目中,新建index.jsp页面,内容如下: ...

  9. 5.10 C++内存管理操作符重载

    参考:http://www.weixueyuan.net/view/6388.html 注意: 内存管理操作符new.new[].delete和delete[]同样也可以进行操作符重载,其重载形式既可 ...

  10. 2.14 C++析构函数

    参考: http://www.weixueyuan.net/view/6345.html 总结: 析构函数就是用于回收创建对象时所消耗的各种资源. 析构函数的调用顺序与构造函数调用顺序正好是相反的. ...