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)
 class MyHashSet {
final ListNode[] nodes = new ListNode[];
/** Initialize your data structure here. */
public MyHashSet() { } public void add(int key) {
int i = idx(key);
ListNode first = nodes[i];
if (first == null) {
nodes[i] = new ListNode(key);
} else if (!exists(first, key)) {
ListNode newNode = new ListNode(key);
newNode.next = nodes[i];
nodes[i] = newNode;
}
} public void remove(int key) {
int i = idx(key);
if (nodes[i] == null) {
return;
}
ListNode current = nodes[i];
ListNode previous = null;
while (current != null) {
if (current.key == key) {
if (previous != null) {
previous.next = current.next;
} else {
nodes[i] = current.next;
}
break;
} else {
previous = current;
current = current.next;
}
}
} /** Returns true if this set contains the specified element */
public boolean contains(int key) {
int i = idx(key);
ListNode first = nodes[i];
if (first == null) {
return false;
}
return exists(first, key);
} int idx(int key) {
return key % nodes.length;
} private boolean exists(ListNode node, int key) {
ListNode current = node;
while (current != null) {
if (current.key == key) {
return true;
}
current = current.next;
}
return false;
}
} class ListNode {
int key;
ListNode next; ListNode(int key) {
this.key = key;
}
}

Design HashSet的更多相关文章

  1. 【Leetcode_easy】705. Design HashSet

    problem 705. Design HashSet 题意: solution1: class MyHashSet { public: /** Initialize your data struct ...

  2. [Swift]LeetCode705. 设计哈希集合 | Design HashSet

    Design a HashSet without using any built-in hash table libraries. To be specific, your design should ...

  3. [LeetCode] Design HashSet 设计HashSet

    Design a HashSet without using any built-in hash table libraries. To be specific, your design should ...

  4. LeetCode 705 Design HashSet 解题报告

    题目要求 Design a HashSet without using any built-in hash table libraries. To be specific, your design s ...

  5. [LeetCode&Python] Problem 705. Design HashSet

    Design a HashSet without using any built-in hash table libraries. To be specific, your design should ...

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

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

  7. C#LeetCode刷题之#705-设计哈希集合​​​​​​​(Design HashSet)

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

  8. 【LeetCode】705. Design HashSet 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位图法 数组法 日期 题目地址:https://le ...

  9. LeetCode算法题-Design HashSet(Java实现)

    这是悦乐书的第298次更新,第317篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第166题(顺位题号是705).不使用任何内建的hash表库设计一个hash集合,应包含 ...

随机推荐

  1. 001_linuxC++之_类的引入

    (一) C++类的引入,图片的程序比较好看,文中程序不贴出来 (二) 知识点 1. 成员函数的存取权限:公有的(public),保护的(protectd),私有的(private) 2. 第27行th ...

  2. js数据类型自动转化规律

    1. 原始数据遇运算符 1. +运算符 1) 如果算子两侧都是字符串,返回拼接结果: 2) 如果算子两侧不全是字符串,则根据实际情况: 如果需要字符串,就调用String(); true + &quo ...

  3. ES6-21.class基本语法

    1.简介(详情参考) class是构造函数的语法糖. class的constructor方法内的实现,就是原来构造函数的实现. class内的所有方法都是在prototype上的,就是原来构造函数的p ...

  4. P1558 色板游戏 状压线段树

    P1558 色板游戏 状压线段树 题面 洛谷P1558 每次不同颜色覆盖一段区间,每次询问一段区间有多少种颜色 因为颜色数\(T\)很小,使用二进制表示状态当前区间有那些颜色,二进制第\(i\)位表示 ...

  5. 【学习笔记】OI模板整理

    CSP2019前夕整理一下模板,顺便供之后使用 0. 非算法内容 0.1. 读入优化 描述: 使用getchar()实现的读入优化. 代码: inline int read() { int x=0; ...

  6. [Oracle]察看一张表的约束 和 察看一张表的索引

    --察看一张表的约束select table_name,constraint_name,constraint_type from user_constraints where table_name=u ...

  7. java -- SSM配置完成后,能访问jsp文件不能访问html文件,报错解析

    SSM配置完成后,能访问jsp文件不能访问html文件,报错解析 在确保路径没有任何问题的,情况下,相同的页面,jsp能够正常访问,html却不能正常访问(404). 解决方法: 在web.xml中添 ...

  8. 《图解 HTTP》读书笔记

    <图解 HTTP>一书是日本学者上野宣所著,2014 年由于均良先生翻译并在国内出版.因为作者使用十分生动的语言和浅显易懂的案例将 HTTP 协议讲解得深入浅出,所以深受开发者喜爱.现在在 ...

  9. 运行React Native项目出现白屏,无法运行

    运行React Native出现白屏,无法运行,查看终端报错如下: 原因: 代码中有语法错误,导致运行失败. 其实到这里可以去Xcode查看控制台打印,会提示哪个文件出现错误的. 解决办法: 找到报错 ...

  10. Spring Cloud(6.1):搭建OAuth2 Authorization Server

    配置web.xml 添加spring-cloud-starter-security和spring-security-oauth2-autoconfigure两个依赖. </dependency& ...