题目要求

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.

题目分析及思路

要求设计一个HashSet(不能使用任何内置库),需要包含以下三个功能:

  1)add(value):往HashSet中插入一个值

  2)contains(value):判断一个值是否存在在HashSet中,若在则返回True,否则返回False

  3)remove(value):从HashSet中移除一个值,若HashSet中不存在该值,则什么也不用做

可以在初始化中添加一个空集合,利用集合的特性完成插入和删除操作。

python代码

class MyHashSet:

def __init__(self):

"""

Initialize your data structure here.

"""

self.s = set()

def add(self, key: int) -> None:

self.s.add(key)

def remove(self, key: int) -> None:

if key in self.s:

self.s.remove(key)

def contains(self, key: int) -> bool:

"""

Returns true if this set contains the specified element

"""

if key in self.s:

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)

转载于:https://www.cnblogs.com/yao1996/p/10687783.html

LeetCode 705 Design HashSet 解题报告相关推荐

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

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

  2. LeetCode刷题记录13——705. Design HashSet(easy)

    LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...

  3. 【LeetCode】3Sum Closest 解题报告

    [题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...

  4. LeetCode Maximum Product Subarray 解题报告

    LeetCode 新题又更新了.求:最大子数组乘积. https://oj.leetcode.com/problems/maximum-product-subarray/ 题目分析:求一个数组,连续子 ...

  5. 【LeetCode】77. Combinations 解题报告(Python C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  6. [leetcode] 28. Implement strStr() 解题报告

    题目链接:https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the fi ...

  7. LeetCode 488 Zuma Game 解题报告

    原文链接: http://hankerzheng.com/blog/Leetcode-Zuma-Game- Problem Description LeetCode 488 Zuma Game Thi ...

  8. 【LeetCode】Palindrome Partitioning 解题报告

    [题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

  9. LeetCode: First Missing Positive 解题报告

    Q: Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2 ...

最新文章

  1. excel图片变成代码_三行代码把女朋友照片变成了素描图片!以为我画的!爱我爱的不行...
  2. Educational Codeforces Round 66 (Rated for Div. 2)
  3. 百度地图API详解之公交导航
  4. 多种IP网络技术的原理和特点
  5. vc6.0中添加快捷注释
  6. Java并发工具包( java.util.concurrent)
  7. 执行yum:Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again...
  8. 调整对话框大小位置以及对话框控件的位置
  9. 程序员简历大曝光,让HR哭笑不得,网友:太老实了!
  10. 通过寄生组合式继承创建js的异常类
  11. idea启动SpringBoot项目自动停止
  12. 图像几何运算——Matlab实现
  13. PC_溢出概念+判断方法+示例
  14. 实体操盘手开发分销商城小程序商业模式解析
  15. Javascript 获得当前文件的url 目录,不含文件名
  16. 四种常用聚类及代码(三):birch(一种层次聚类)
  17. mount point / 挂载点
  18. 九宫山自驾露营二日游
  19. TabLayout单独使用
  20. java测试题一附答案,Java考试题30道(附答案)

热门文章

  1. Java中常用集合类对比_集合比较
  2. Linux 命令之 du -- 显示每个文件和目录的磁盘使用空间/所占用的磁盘空间大小/所使用的磁盘空间大小/查看文件和目录的大小
  3. Linux 查看数据库MySQL安装文件和安装目录的命令
  4. MySQL的索引学习
  5. No identifier specified for entity没有为实体指定标识符
  6. 数组索引必须为正整数或逻辑值_Office 365函数新世界——动态数组
  7. 怎样自动提取邮件的内容_这些最新的外贸搜索开发工具(图灵搜、谷歌搜索提取工具、易查查),你会使用吗?...
  8. LeetCode 268 缺失数字
  9. php7.1 split,PHP 函数 split()
  10. java json注解_返回json用什么注解