
【java】705. 设计哈希集合---用定长数组代替简单的HashSet!!!
发布日期:2021-05-07 02:22:03
浏览次数:19
分类:精选文章
本文共 1041 字,大约阅读时间需要 3 分钟。
不使用任何内建的哈希表库设计一个哈希集合(HashSet)。
实现 MyHashSet 类:
void add(key) 向哈希集合中插入值 key 。
bool contains(key) 返回哈希集合中是否存在这个值 key 。 void remove(key) 将给定值 key 从哈希集合中删除。如果哈希集合中没有这个值,什么也不做。示例:
输入:
[“MyHashSet”, “add”, “add”, “contains”, “contains”, “add”, “contains”, “remove”, “contains”] [[], [1], [2], [1], [3], [2], [2], [2], [2]] 输出: [null, null, null, true, false, null, true, null, false]解释:
MyHashSet myHashSet = new MyHashSet(); myHashSet.add(1); // set = [1] myHashSet.add(2); // set = [1, 2] myHashSet.contains(1); // 返回 True myHashSet.contains(3); // 返回 False ,(未找到) myHashSet.add(2); // set = [1, 2] myHashSet.contains(2); // 返回 True myHashSet.remove(2); // set = [1] myHashSet.contains(2); // 返回 False ,(已移除)提示:
0 <= key <= 106
最多调用 104 次 add、remove 和 contains 。代码:private boolean []a=null; public MyHashSet() { a=new boolean[1000001]; } public void add(int key) { a[key]=true; } public void remove(int key) { a[key]=false; } public boolean contains(int key) { return a[key]; }
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年03月30日 11时41分53秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
常用Windows 快捷键
2021-05-09
linux命令-压缩与打包
2021-05-09
ORACLE 11g 生产中高水位线(HWM)处理
2021-05-09
centos 6.x 编译安装 pgsql 9.6
2021-05-09
weblogic 服务器部署SSL证书
2021-05-09
oracle 11g not in 与not exists 那个高效?
2021-05-09
Linux 安装Redis 5.0(以及参数调优)
2021-05-09
html5 Game开发系列文章之 零[开篇]
2021-05-09
为什么阿里巴巴建议集合初始化时,指定集合容量大小
2021-05-09
为什么阿里巴巴要求谨慎使用ArrayList中的subList方法
2021-05-09
Redis不是一直号称单线程效率也很高吗,为什么又采用多线程了?
2021-05-09
基于Python的Appium环境搭建合集
2021-05-09
Requests实践详解
2021-05-09
接口测试简介
2021-05-09
Golang Web入门(4):如何设计API
2021-05-09
让sublime实现js控制台(前提是安装了nodejs)
2021-05-09
树莓派连接二手液晶屏小记
2021-05-09
error: 'LOG_TAG' macro redefined
2021-05-09