LeetCode 705:设计哈希集合 Design HashSet
题目:
不使用任何内建的哈希表库设计一个哈希集合
具体地说,你的设计应该包含以下的功能
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的更多相关文章
- Java实现 LeetCode 705 设计哈希集合(使用数组保存有没有被用过)
705. 设计哈希集合 不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 add(value):向哈希集合中插入一个值. contains(value) :返回哈希集合中 ...
- [Swift]LeetCode705. 设计哈希集合 | Design HashSet
Design a HashSet without using any built-in hash table libraries. To be specific, your design should ...
- LeetCode 706:设计哈希映射 Design HashMap
题目: 不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get ...
- Java实现 LeetCode 706 设计哈希映射(数组+链表)
706. 设计哈希映射 不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新 ...
- LeetCode 705. Design HashSet (设计哈希集合)
题目标签:HashMap 题目让我们设计一个 hashset,有add,contains,remove 功能. 建立一个boolean array,index 是数字的值,具体看code. Java ...
- [Swift]LeetCode706. 设计哈希映射 | Design HashMap
Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...
- 领扣(LeetCode)设计哈希映射 个人题解
不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get(key ...
- C#LeetCode刷题-哈希表
哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串 24.2% 中等 18 四数之和 ...
- C#LeetCode刷题之#705-设计哈希集合(Design HashSet)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4114 访问. 不使用任何内建的哈希表库设计一个哈希集合 具体地说 ...
随机推荐
- redis5.0.0功能介绍以及主从集群、哨兵搭建
这两天突然想起redis,索性就再尝试一下搭建最新版本的redis,过程有点艰辛呀,记录一下,供自己和大家今后搭建做参考. 一.为什么用Redis? 我自己总结了一下: 1.基于内存实现的key-va ...
- Red Hat Enterprise Linux 官方正式版镜像下载
Red Hat Enterprise Linux是美国红帽公司开发的商业市场导向的Linux发行版,为方便大家学习研究,整理分享历代红帽官方正式版镜像给有需要的朋友们. 下载地址:https://ww ...
- 查看/运行jpynb文件
Windows OS:安装好pip包,能使用pip进行安装第三方包. cmd命令行: pip install jupyter jupyter notebook cd (xx.jpynb文件所在文件夹) ...
- 因果推理的春天系列序 - 数据挖掘中的Confounding, Collidar, Mediation Bias
序章嘛咱多唠两句.花了大半个月才反反复复,断断续续读完了图灵奖得主Judea Pearl的The Book of WHY,感觉先读第四章的案例会更容易理解前三章相对抽象的内容.工作中对于归因问题迫切的 ...
- Java基础 - volatile
volatile的作用:对与volatile修饰的变量, 1,保证该变量对所有线程的可见性. 2,禁止指令重排序. Java内存模型(JMM) 原子性 i = 2; 把i加载到工作内存副本i,副本i= ...
- Unity ugui屏幕适配与世界坐标到ugui屏幕坐标的转换
我们知道,如今的移动端设备分辨率五花八门,而开发过程中往往只取一种分辨率作为设计参考,例如采用1920*1080分辨率作为参考分辨率. 选定了一种参考分辨率后,美术设计人员就会固定以这样的分辨率来设计 ...
- PHP ThinkPHP 非常好用的增删改查方法
获取列表数据(多条) /*** 获取页面列表* @param $params //查询条件 例:['id'=>['in','1,2']] ['status'=>1]* @param arr ...
- PAT 1006 Sign In and Sign Out 查找元素
At the beginning of every day, the first person who signs in the computer room will unlock the door, ...
- SpringCloud中Feign的适配器的实现方案
前言 最近在做微服务的项目,各个系统之间需要进行调用,然后用一个适配器来实现服务之间的feign调用,使用适配器进行统一管理. 实现方案 首先我们需要将服务的名称进行单独的配置,可以方便的进行切换和扩 ...
- C# 从图片中截取一部分图片,并返回所截取的图片
/// <summary> /// 从图片中截取一部分图片 /// </summary> /// <param name="fromImagePath" ...