题目:

不使用任何内建的哈希表库设计一个哈希集合

具体地说,你的设计应该包含以下的功能

  • add(value):向哈希集合中插入一个值。
  • contains(value) :返回哈希集合中是否存在这个值。
  • remove(value):将给定值从哈希集合中删除。如果哈希集合中没有这个值,什么也不做。

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.

示例:

MyHashSet hashSet = new MyHashSet();
hashSet.add(1);
hashSet.add(2);
hashSet.contains(1); // 返回 true
hashSet.contains(3); // 返回 false (未找到)
hashSet.add(2);
hashSet.contains(2); // 返回 true
hashSet.remove(2);
hashSet.contains(2); // 返回 false (已经被删除)

注意:

  • 所有的值都在 [1, 1000000]的范围内。
  • 操作的总数目在[1, 10000]范围内。
  • 不要使用内建的哈希集合库。

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.

解题思路:

​ 题目明确限定了数据大小和数据集大小,都在int整型范围内,所以最简单的解法就是,以一个长度为10000001布尔类型 数组,索引位置就是数据值大小。True、False代表哈希集合内是否有该数。这应该是最简单的哈希散列函数了:y = x

代码:

Java:

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

Python:

class MyHashSet:

    def __init__(self):
"""
Initialize your data structure here.
"""
self.hash_set = [False]*1000001 def add(self, key: int) -> None:
self.hash_set[key] = True def remove(self, key: int) -> None:
self.hash_set[key] = False def contains(self, key: int) -> bool:
"""
Returns true if this set contains the specified element
"""
return self.hash_set[key]

欢迎关注微.信公.众号:爱写Bug

LeetCode 705:设计哈希集合 Design HashSet的更多相关文章

  1. Java实现 LeetCode 705 设计哈希集合(使用数组保存有没有被用过)

    705. 设计哈希集合 不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 add(value):向哈希集合中插入一个值. contains(value) :返回哈希集合中 ...

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

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

  3. LeetCode 706:设计哈希映射 Design HashMap

    题目: 不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get ...

  4. Java实现 LeetCode 706 设计哈希映射(数组+链表)

    706. 设计哈希映射 不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新 ...

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

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

  6. [Swift]LeetCode706. 设计哈希映射 | Design HashMap

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

  7. 领扣(LeetCode)设计哈希映射 个人题解

    不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get(key ...

  8. C#LeetCode刷题-哈希表

    哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串   24.2% 中等 18 四数之和   ...

  9. C#LeetCode刷题之#705-设计哈希集合​​​​​​​(Design HashSet)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4114 访问. 不使用任何内建的哈希表库设计一个哈希集合 具体地说 ...

随机推荐

  1. mysql实践:sql优化

    ---恢复内容开始--- 设计表的时候 1. 不同的表涉及同一个公共意义字段不要使用不同的数据类型(可能导致索引不可用,查询结果有偏差) 2. 不要一张表放太多的数据     主表20~30个字段   ...

  2. div块水平居中,垂直居中

    水平居中一个div想要水平居中于它的父div中只需要给它加css属性margin:0 auto; 即可 <!DOCTYPE html> <html> <head> ...

  3. 用iText5-2-其他类型PDF

    //设置文件属性的PDF package com.wf.zhang.test; import java.io.FileOutputStream; import com.itextpdf.text.Ba ...

  4. 关于Git和GitHub的一些知识

    git是分布式的版本控制工具,可离线,svn是集中式的,要联网操作.集中式的所有数据都放在服务器端,如果服务器宕机,则历史记录也可能就丢失了,这叫做单点故障.分布式的数据可直接保存在客户端. 为何要版 ...

  5. Hive参数调优

    调优 Hive提供三种可以改变环境变量的方法,分别是: (1)修改${HIVE_HOME}/conf/hive-site.xml配置文件: 所有的默认配置都在${HIVE_HOME}/conf/hiv ...

  6. 对于jenkins上前端项目 npm 或 yarn 打包 及 部署 使用

    1.npm和yarn镜像源地址 npmMirror ~~~~~ https://skimdb.npmjs.com/registry/ edunpm ~~~~~~ http://registry.enp ...

  7. 1025 PAT Ranking 双重排序

    Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhe ...

  8. Web前端基础(18):jQuery基础(五)

    1. 事件 前面我们介绍完js的事件流的概念之后,相信大家对事件流也有所了解了.那么接下来我们看一下jquery的事件操作. 在说jquery的每个事件之前,我们先来看一下事件对象 . 1.1 事件对 ...

  9. Unitest自动化测试基于HTMLTestRunner报告案例

    报告效果如下: HTMLTestRunner脚本代码如下: #coding=utf-8 # URL: http://tungwaiyip.info/software/HTMLTestRunner.ht ...

  10. 当您解开后您从 Internet 上下载的压缩的文件时,文件的修改日期更改为您提取它的日期

    用鼠标右键单击该压缩的文件,然后单击属性. 单击常规选项卡,请单击取消阻止,然后单击确定. 从压缩文件中提取文件.