Design 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)
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的更多相关文章
- 【Leetcode_easy】705. Design HashSet
problem 705. Design HashSet 题意: solution1: class MyHashSet { public: /** Initialize your data struct ...
- [Swift]LeetCode705. 设计哈希集合 | Design HashSet
Design a HashSet without using any built-in hash table libraries. To be specific, your design should ...
- [LeetCode] Design HashSet 设计HashSet
Design a HashSet without using any built-in hash table libraries. To be specific, your design should ...
- LeetCode 705 Design HashSet 解题报告
题目要求 Design a HashSet without using any built-in hash table libraries. To be specific, your design s ...
- [LeetCode&Python] Problem 705. Design HashSet
Design a HashSet without using any built-in hash table libraries. To be specific, your design should ...
- LeetCode 705:设计哈希集合 Design HashSet
题目: 不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 add(value):向哈希集合中插入一个值. contains(value) :返回哈希集合中是否存在这个值. ...
- C#LeetCode刷题之#705-设计哈希集合(Design HashSet)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4114 访问. 不使用任何内建的哈希表库设计一个哈希集合 具体地说 ...
- 【LeetCode】705. Design HashSet 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位图法 数组法 日期 题目地址:https://le ...
- LeetCode算法题-Design HashSet(Java实现)
这是悦乐书的第298次更新,第317篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第166题(顺位题号是705).不使用任何内建的hash表库设计一个hash集合,应包含 ...
随机推荐
- 02_通过位置变量创建 Linux 系统账户及密码
#!/bin/bash#$1 是执行脚本的第一个参数,$2 是执行脚本的第二个参数useradd "$1"echo "$2" | passwd --stdin ...
- ueditor+word图片上传
最近公司做项目需要实现一个功能,在网页富文本编辑器中实现粘贴Word图文的功能. 我们在网站中使用的Web编辑器比较多,都是根据用户需求来选择的.目前还没有固定哪一个编辑器 有时候用的是UEditor ...
- 2018 Nowcoder Multi-University Training Contest 2
目录 Contest Info Solutions A. run D. monrey G. transform H. travel I. car J. farm Contest Info Practi ...
- rxjs入门指南
使用场景 在复杂的,频繁的异步请求场景,使用rxjs. 在依赖的多个异步数据,决定渲染的情景,使用rxjs. 总之:在前台频繁的.大量的.和后台数据交互的复杂项目里面,使用rxjs(web端,iOS, ...
- idea svn设置忽略提交文件
1.找到版本控制位置 2.新建变动列表(装载忽略的文件内容) 3. 将默认的变动列表中需要忽略的文件拖入ignored列表下 4. 提交时,选择default即可. 设置完毕之后,可以在提交文件时将之 ...
- 20175313 张黎仙《Java程序设计》第十一周学习总结
目录 一.教材学习内容总结 二.教材学习中的问题和解决过程 三.代码托管 四.心得体会 五.学习进度条 六.参考资料 一.教材学习内容总结 第十三章内容 主要内容 URL类 InetAdress类 套 ...
- mysql 误删除所有用户或者忘记root密码
/etc/init.d/mysqld stop //停止数据库/etc/init.d/mysqld restart //启动数据库(1)开启特殊启动模式mysqld_safe --skip-grant ...
- 通过 redo日志恢复数据库
如果还原存档的重做日志文件和数据文件,则必须先执行介质恢复,然后才能打开数据库.归档重做日志文件中未反映在数据文件中的任何数据库事务都将应用于数据文件,从而在打开数据库之前将它们置于事务一致状态. 介 ...
- 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_21-CMS前端页面查询开发-Api调用
在api里面定义js方法获取服务端的数据 把home模块的下的home.js文件复制到api的目录下改名叫做cms.js 复制过来 默认的代码 实际上引用的是base下的api/public.js文件 ...
- tomcat 8.xxx ssl使用websocket Failed to close the ServletOutputStream connection cleanly
报错: 2019-09-17 10:30:17.277 default [https-jsse-nio-8110-exec-20] INFO o.a.t.w.s.WsRemoteEndpointImp ...