问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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
False True
False
True
False

分析:

MyHashSet1的解法实在是可耻至极,居然在CSDN找到一大片,这根本违背了该道题的设计意图,并且其中的Add、Remove和Contains三个方法通过查看微软的源代码发现均达到了线性时间复杂度。既然是要求我们设计哈希集合,我们应该要达到常量的时间复杂度才有意义。

MyHashSet2的解法是一个典型的空间换时间的解法,更为贴合原题意,并且其中的Add、Remove和Contains三个方法均为  的时间复杂度。

C#LeetCode刷题之#705-设计哈希集合​​​​​​​(Design HashSet)的更多相关文章

  1. LeetCode 705:设计哈希集合 Design HashSet

    题目: 不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 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. Java实现 LeetCode 705 设计哈希集合(使用数组保存有没有被用过)

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

  4. C#LeetCode刷题之#706-设计哈希映射(Design HashMap)

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

  5. C#LeetCode刷题之#622-设计循环队列​​​​​​​(Design Circular Queue)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4126 访问. 设计你的循环队列实现. 循环队列是一种线性数据结构 ...

  6. C#LeetCode刷题-设计

    设计篇 # 题名 刷题 通过率 难度 146 LRU缓存机制   33.1% 困难 155 最小栈 C#LeetCode刷题之#155-最小栈(Min Stack) 44.9% 简单 173 二叉搜索 ...

  7. C#LeetCode刷题-哈希表

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

  8. C#LeetCode刷题-队列

    队列篇 # 题名 刷题 通过率 难度 363 矩形区域不超过 K 的最大数值和   27.2% 困难 621 任务调度器   40.9% 中等 622 设计循环队列 C#LeetCode刷题之#622 ...

  9. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

随机推荐

  1. 阿里面试官:这些软件测试面试题都答对了,I want you!

    [ 你悄悄来,请记得带走一丝云彩 ] 测试岗必知必会 01请描述如何划分缺陷与错误严重性和优先级别? 给软件缺陷与错误划分严重性和优先级的通用原则: 1. 表示软件缺陷所造成的危害和恶劣程度. 2. ...

  2. P2058 海港 (洛谷)

    这个题复制过来真的有点恶心,懒得手打,以后再搬题面吧. 今天我双更了,AC这个题我就完成某谷春令营第一课的作业了(假的) 这个题是个双指针.非常友善.一直往里读入就可以了,遇见不是一条船的乘客输出这一 ...

  3. 在 CentOS 7(Linux)上部署ASP.NET Core 2.2 Web应用程序(Tengine、Asp.Net Core MVC、Centos 7、MySql)

    一.前言 1.简单记录一下Linux CentOS 7中安装与配置Tengine的详细步骤. 2.简单比较一下Tengine 和Nginx 3.搭建Asp.net Core和部署 Web程序 4.总结 ...

  4. C# 判断和创建目录路径

    在进行一些导出或下载时,需要创建一个本地路径,以供文件进行下载和创建. if (Directory.Exists(Server.MapPath("~/upimg/hufu")) = ...

  5. OKex平台如何使用谷歌身份验证?

    打开OK交易所官网,找到谷歌身份验证器的开启界面 登陆后点击右上角头像-账户和安全 然后[安全设置]里出现“谷歌验证”的位置,点击开启按钮,到了二维码和密钥显示的界面 我们不使用谷歌身份验证器,因为需 ...

  6. 利用74HC595实现的流水灯 Arduino

    int big = 2; int push = 3; int datain = 4; void setup() { Serial.begin(9600); pinMode(big, OUTPUT); ...

  7. Java基础之java8新特性(1)Lambda

    一.接口的默认方法.static方法.default方法. 1.接口的默认方法 在Java8之前,Java中接口里面的默认方法都是public abstract 修饰的抽象方法,抽象方法并没有方法实体 ...

  8. 前端学习(十):CSS选择器

    进击のpython ***** 前端学习--CSS选择器 每一条CSS样式声明由两部分组成: 选择器{ 样式: } 在CSS中{}之前的部分就是"选择器","选择器&qu ...

  9. Js数组对象的属性值升序排序,并指定数组中的某个对象移动到数组的最前面

    需求整理: 本篇文章主要实现的是将一个数组的中对象的属性值通过升序的方式排序,然后能够让程序可以指定对应的数组对象移动到程序的最前面. 数组如下所示: var arrayData= [{name: & ...

  10. Git 提交、删除、切换命令

    1.将本地代码提交到远程仓库 [初始将文件修改上传到远程仓库] 初始化: git init 添加到暂存区: git  add . 提交到仓库: git commit -m 'first commit' ...