[LeetCode] Design HashSet 设计HashSet
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.
这道题让我们设计HashSet,不能用自带的哈希表的数据结构,而且要我们主要实现添加,删除,以及判断是否存在,这三个函数。我们都知道HashSet最有用的地方就是其能够在常数的时间内判断某个元素是否存在,这都得归功于哈希表的作用。但是现在不让我们用了,但我们还是得保证其常数级的查找效率,那么就用空间来换时间吧。既然题目中说了数字的范围不会超过1000000,那么我们就申请这么大空间的数组,这样对于在HashSet中的数字,我们就将其标记为1,不在或者删除了的就标记为0,检测的时候就看其值是否为1即可,参见代码如下:
解法一:
class MyHashSet {
public:
/** Initialize your data structure here. */
MyHashSet() {
data.resize(, );
}
void add(int key) {
data[key] = ;
}
void remove(int key) {
data[key] = ;
}
/** Returns true if this set contains the specified element */
bool contains(int key) {
return data[key] == ;
}
private:
vector<int> data;
};
我们可以来适度的优化一下空间复杂度,由于存入HashSet的数字也许不会跨度很大,那么直接就申请长度为1000000的数组可能会有些浪费,那么我们其实可以使用1000个长度为1000的数组来代替,那么就要用个二维数组啦,实际上开始我们只申请了1000个空数组,对于每个要处理的元素,我们首先对1000取余,得到的值就当作哈希值,对应我们申请的那1000个空数组的位置,在加入元素时,一旦计算出了哈希值,我们将对应的空数组resize为长度1000,然后根据哈希值和key/1000来确定具体的加入位置。移除数字一样的,先计算出哈希值,如果对应的数组不为空的话,找到对应的位置并赋值为0。不过大家也可以看出来,我们在加入元素时会开辟1000的新空间,但是删除这个元素时,并没有检测这1000个位置是否均为0,是的话应该删除这1000个新空间。但是这样可能会使得删除函数变慢一些,参见代码如下:
解法二:
class MyHashSet {
public:
/** Initialize your data structure here. */
MyHashSet() {
data.resize(, vector<int>());
}
void add(int key) {
int hashKey = key % ;
if (data[hashKey].empty()) {
data[hashKey].resize();
}
data[hashKey][key / ] = ;
}
void remove(int key) {
int hashKey = key % ;
if (!data[hashKey].empty()) {
data[hashKey][key / ] = ;
}
}
/** Returns true if this set contains the specified element */
bool contains(int key) {
int hashKey = key % ;
return !data[hashKey].empty() && data[hashKey][key / ];
}
private:
vector<vector<int>> data;
};
类似题目:
参考资料:
https://leetcode.com/problems/design-hashset/
https://leetcode.com/problems/design-hashset/discuss/185826/C%2B%2B-solution
https://leetcode.com/problems/design-hashset/discuss/193132/Solution-without-boolean-array
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Design HashSet 设计HashSet的更多相关文章
- [LeetCode] Design HashMap 设计HashMap
Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...
- [LeetCode] Design Twitter 设计推特
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...
- [LeetCode] Design Tic-Tac-Toe 设计井字棋游戏
Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the fol ...
- [LeetCode] Design TinyURL 设计精简URL地址
Note: For the coding companion problem, please see: Encode and Decode TinyURL. How would you design ...
- LeetCode 622:设计循环队列 Design Circular Queue
LeetCode 622:设计循环队列 Design Circular Queue 首先来看看队列这种数据结构: 队列:先入先出的数据结构 在 FIFO 数据结构中,将首先处理添加到队列中的第一个元素 ...
- LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组)
LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组) 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:h ...
- LeetCode初级算法--设计问题02:最小栈
LeetCode初级算法--设计问题02:最小栈 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net ...
- 使用MATLAB 2019 App Design 工具设计一个 电子日记App
使用MATLAB 2019 App Design 工具设计一个 电子日记App1.1 前言:由于信号与系统课程需要,因此下载了MATLAB软件,加之对新款的执着追求,通过一些渠道,下载了MATLAB ...
- Design Principle vs Design Pattern 设计原则 vs 设计模式
Design Principle vs Design Pattern设计原则 vs 设计模式 来源:https://www.tutorialsteacher.com/articles/differen ...
随机推荐
- JavaScript IIEF 模仿块级作用域
前言 JavaScript没有块级作用域的概念.但是通过IIEF 立即执行函数我们可以实现块级作用域. function outputNumbers(count){ for (var i=0; i & ...
- Mysql漏洞修复方法思路及注意事项
[系统环境] 系统环境:Red Hat Enterprise Linux Server release 5.4 (Tikanga) + 5.7.16 MySQL Community Server ...
- SQL Server 跨服务器操作
Ø 简介 在工作中编写 SQL 时经常会遇到跨库或跨服务器操作,比如查询时,通过 A 服务器的某张表关联 B 服务器某张表,进行连接查询.或者从另一台服务器中的数据,对当前数据库中的数据进行 CRU ...
- 使用sessionStorage、localStorage存储数组与对象
先介绍一下localStorage localStorage对象是HTML5的客户端存储持久化数据的方案.为了能访问到同一个localStorage对象,页面必须来自同一个域名(子域名无效),使用同一 ...
- L1-Day2
L1-Day21.明智的人从来不生气. [我的翻译]A wise man is never angry. [标准答案]A wise man never gets angry. [对比分析]be和get ...
- 老师博客copy -高阶函数2
新闻 管理 Py西游攻关之函数 一 函数是什么? 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,具体区别,我们后面会讲,编程中的函数在英文中也有很多不同的叫法.在B ...
- vue之生命周期钩子函数之运用
一.什么是生命周期钩子函数: 每个 Vue 实例在被创建时都要经过一系列的初始化过程——例如,需要设置数据监听.编译模板.将实例挂载到 DOM 并在数据变化时更新 DOM 等.同时在这个过程中也会运行 ...
- nginx问题相关记录
nginx目前主要用来做反向代理和负载均衡,其实它也可以是一个web服务器: 1.反向代理: location /api/ { proxy_next_upstream error timeout ht ...
- JavaScript高级程序设计(读书笔记)(一)
本笔记汇总了作者认为“JavaScript高级程序设计”这本书的前七章知识重点,仅供参考. 第一章 JavaScript简介 JavaScript发展简史: 1995年,JavaScript诞生 19 ...
- chromerdriver下载地址:xpath_help
chrome下载地址 http://npm.taobao.org/mirrors/chromedriver xpath_help https://blog.csdn.net/Cayny/article ...