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 访问. 不使用任何内建的哈希表库设计一个哈希集合 具体地说 ...
随机推荐
- 49-在 overlay 中运行容器
上一节我们创建了 overlay 网络 ov_net1,今天将运行一个 busybox 容器并连接到 ov_net1: 查看容器的网络配置: bbox1 有两个网络接口 eth0 和 eth1.eth ...
- C语言编程的一些小总结
1. static:可用于定义静态局部变量 在局部变量前,加上关键字static,该变量就被定义成为一个静态局部变量. 举一个静态局部变量的例子: void fn() { static int n=1 ...
- bayaim——达梦数据库安装DM7-单实例-Centos7.txt
bay——安装_达梦DM7-单实例-Centos7.txt IP:10.20.100.22 ---------------------------------------------1.下载 注意:要 ...
- Linux防火墙常用命令
Centos7 查看防火墙状态 sudo firewall-cmd --state 输出running则表示防火墙开启,反之则是关闭,也可以使用下面命令进行查询 sudo systemctl stat ...
- celery配置
celery配置 celery的官方文档其实相对还是写的很不错的.但是在一些深层次的使用上面却显得杂乱甚至就没有某些方面的介绍, 通过我的一个测试环境的settings.py来说明一些使用celery ...
- 电位器控制两个 LED 灯交替闪烁
电路图: 布局:
- VirtualBox中重建Host-Only网卡后无法启动虚拟机
问题: 在删除原有VirtualBox Host-Only虚拟网卡并重新添加后,虚拟机可能会无法启动,出现以下错误 Failed to open/create the internal network ...
- js之好看的鼠标点击-光标特效
1.光标特效 <script src="https://blog-static.cnblogs.com/files/axqa/bubbleCursor.js">< ...
- Mysql数据基本操作(增、删、改、查)
一.数据库配置 # 通过配置文件统一配置的目的: 统一管理 服务端(mysqld).客户端(client) 1.配置mysqld(服务端)的编码为utf-8,再创建数据库的时候,默认编码都采用了utf ...
- DataTable实现分组
有时候我们从数据库中查询出来数据之后,需要按照DataTable的某列进行分组,可以使用下面的方法实现,代码如下: using System; using System.Collections.Gen ...