问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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三个方法均为  的时间复杂度。

最新文章

  1. Sublime Text 3 高效编码快捷键
  2. Cocos2d-x 3.X手游开发实例详解
  3. PostgreSQL 同步复制(1master+2standby)
  4. julia解无忧公主的数学时间097.jl
  5. .NET文件上传的大小限制配置
  6. Maven镜像配置
  7. 把测试app打包成ipa文件
  8. 学习笔记 broswerify + watchify + beefy
  9. Android --- 读取系统资源函数getResources()小结
  10. Java之GC
  11. 可编辑且宽度自适应input
  12. 【转】Esp8266学习之旅① 搭建开发环境,开始一个“hellow world”串口打印。
  13. 运维yum搭建zabbix
  14. Spark笔记-repartition和coalesce
  15. Beta阶段冲刺---Day5
  16. mybatis 使用oracle merge into 语句踩坑实录
  17. DRF序列化/反序列化
  18. LeetCode 88. 合并两个有序数组
  19. django HttpResponse对象
  20. iOS UI-UIScrollView控件实现图片缩放功能

热门文章

  1. Java常用API(Scanner类)
  2. INS-40718 和 INS - 30516
  3. javascript : 找到一个树型数据的一个节点及其所有父节点
  4. echarts 踩坑 : 为什么触摸柱状图的时后柱子不见了?原来是color的锅!
  5. 利用tox打造自动自动化测试框架
  6. ‘100%’wuxiao
  7. 新款iPad Pro4的电池续航和充电速度对比
  8. 将音频文件转二进制分包存储到Redis(奇淫技巧操作)
  9. 【扩展推荐】Intervention/image 图片处理
  10. python基础day7_购物车实例