C#LeetCode刷题之#705-设计哈希集合(Design HashSet)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4114 访问。
不使用任何内建的哈希表库设计一个哈希集合
具体地说,你的设计应该包含以下的功能
add(value):向哈希集合中插入一个值。
contains(value) :返回哈希集合中是否存在这个值。
remove(value):将给定值从哈希集合中删除。如果哈希集合中没有这个值,什么也不做。
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]范围内。
不要使用内建的哈希集合库。
Design a HashSet without using any built-in hash table libraries.
To be specific, your design should include these two 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); // 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 [1, 1000000].
The number of operations will be in the range of [1, 10000].
Please do not use the built-in HashSet library.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4114 访问。
public class Program {public static void Main(string[] args) {var hashSet = new MyHashSet1();var hashSet2 = new MyHashSet2();hashSet.Add(1);hashSet.Add(2);hashSet.Contains(1);hashSet.Contains(3);hashSet.Add(2);hashSet.Contains(2);hashSet.Remove(2);hashSet.Contains(2);Console.WriteLine();hashSet2.Add(1);hashSet2.Add(2);hashSet2.Contains(1);hashSet2.Contains(3);hashSet2.Add(2);hashSet2.Contains(2);hashSet2.Remove(2);hashSet2.Contains(2);Console.ReadKey();}public class MyHashSet1 {//此解法实在可耻!仅作演示private List<int> _list = null;public MyHashSet1() {_list = new List<int>();}public void Add(int key) {if(!_list.Contains(key)) {_list.Add(key);}}public void Remove(int key) {_list.Remove(key);}public bool Contains(int key) {var res = _list.Contains(key);Console.WriteLine(res);return res;}}public class MyHashSet2 {private int[] buckets;public MyHashSet2() {buckets = new int[1000000];}private int GetHashCode(int key) {//用本身值作为散列值return key;}public void Add(int key) {buckets[GetHashCode(key)] = 1;}public void Remove(int key) {buckets[GetHashCode(key)] = 0;}public bool Contains(int key) {var res = buckets[GetHashCode(key)] == 1;Console.WriteLine(res);return res;}}}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4114 访问。
True
False
True
FalseTrue
False
True
False
分析:
MyHashSet1的解法实在是可耻至极,居然在CSDN找到一大片,这根本违背了该道题的设计意图,并且其中的Add、Remove和Contains三个方法通过查看微软的源代码发现均达到了线性时间复杂度。既然是要求我们设计哈希集合,我们应该要达到常量的时间复杂度才有意义。
MyHashSet2的解法是一个典型的空间换时间的解法,更为贴合原题意,并且其中的Add、Remove和Contains三个方法均为 的时间复杂度。
C#LeetCode刷题之#705-设计哈希集合(Design HashSet)相关推荐
- 705. 设计哈希集合
链接:705. 设计哈希集合 题解:https://leetcode-cn.com/problems/design-hashset/solution/she-ji-ha-xi-ji-he-by-lee ...
- leetcode 705. 设计哈希集合
不使用任何内建的哈希表库设计一个哈希集合(HashSet). 实现 MyHashSet 类: void add(key) 向哈希集合中插入值 key . bool contains(key) 返回哈希 ...
- 力扣题目——705. 设计哈希集合
注:本文的代码实现使用的是 JS(JavaScript),为前端中想使用JS练习算法和数据结构的小伙伴提供解题思路. 描述 不使用任何内建的哈希表库设计一个哈希集合(HashSet). 实现 MyHa ...
- 卷进大厂系列之LeetCode刷题笔记:设计链表(中等)
学算法,刷力扣,加油卷,进大厂! 题目描述 力扣题目链接 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下 ...
- C#LeetCode刷题之#706-设计哈希映射(Design HashMap)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4116 访问. 不使用任何内建的哈希表库设计一个哈希映射 具体地说 ...
- C#LeetCode刷题之#622-设计循环队列(Design Circular Queue)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4126 访问. 设计你的循环队列实现. 循环队列是一种线性数据结构 ...
- 705.设计哈希集合
题目: 思路: 也就是要维护一个二维动态数组来存储这些数据,要注意的是LinkedList中有三种remove()方法 默认使用的是根据索引来移除元素 因此这时,我们要把每个函数的形参都设置为Inte ...
- C#LeetCode刷题-设计
设计篇 # 题名 刷题 通过率 难度 146 LRU缓存机制 33.1% 困难 155 最小栈 C#LeetCode刷题之#155-最小栈(Min Stack) 44.9% 简单 173 二叉搜索树迭 ...
- C#LeetCode刷题-哈希表
哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串 24.2% 中等 18 四数之和 ...
最新文章
- tvm模型部署c++ 分析
- linux查端口占用
- GCC编译选项 -OX[转]
- robot:linux下安装robot环境
- 使用python下载文件的简单示例_Python
- 云计算OpenStack:云在身边博客园
- c语言有开始菜单的flybird,C语言实现Flybird
- 在Windows环境下,将tomcat的默认端口修改为8081
- 企业双运营商出口负载均衡同时冗余备份
- ----化学方程式配平工具----
- unity图片指定任意不规则区域显示
- 刚挣钱的程序员同学该如何花钱?
- C++ Primer Plus(第六版)编程练习答案 第3章 处理数据
- CODE CHINA
- python爬虫需要cookie_python爬虫(六) Cookie
- 互联网创业,也许一开始就不是草根的天下
- Directory常用方法,不积硅步无以至千里
- C语言,根据某员工的销售额计算销售提成,销售额小于1万,提成为5%,销售额大于1万,小于等于2万,提成为8%;售额大于2万,提成为10%。
- php 实线,PHP实现的功能是显示8条基色色带
- 二手笔记本中常见三叉插头以及英标欧标和美标的区别!