LeetCode刷题记录13——705. Design HashSet(easy)
LeetCode刷题记录13——705. Design HashSet(easy)
目录
LeetCode刷题记录13——705. Design HashSet(easy)
前言
题目
语言
思路
源码
后记
前言
今天贼热
题目
今天的题目是关于哈希表的,叫你创建一个哈希表,但是不能用他封装好的。
语言
C++
思路
首先他已经指出不能使用封装好的hashmap(如果使用的话,那就没意义了)。所以我一开始想,上一题用的两个栈来实现队列,这次也可以用栈来实现哈希表。结果运行超时,我就???,后来发现根本不用那么麻烦,既然都想到栈了,何不如在想简单一点,直接用数组呢。。。
所以先定义一个数组a,初值置为随便一个负数。
void add(int key):直接就数组下标和值都用key就好了,所以前提是数组大小得设置够。
void remove(int key):直接令a[key]=一个负数就好了,这样就不能一一对应,也就相当于把他remove
bool contains(int key):看他存不存在,直接查询是否有a[key]==key,如果有就返回true,否则返回false。
源码
class MyHashSet {
public:/** Initialize your data structure here. */
long a[1000009]={-10};MyHashSet() {}void add(int key) {a[key]=key;}void remove(int key) {a[key]=-1;}/** Returns true if this set contains the specified element */bool contains(int key) {if(a[key]==key)return true;return false;}
};
/*** Your MyHashSet object will be instantiated and called as such:* MyHashSet obj = new MyHashSet();* obj.add(key);* obj.remove(key);* bool param_3 = obj.contains(key);*/
后记
今天的哈希表还算简单,其实这些都是数组。
LeetCode刷题记录13——705. Design HashSet(easy)相关推荐
- wy的leetcode刷题记录_Day15
wy的leetcode刷题记录_Day15 目录 wy的leetcode刷题记录_Day15 2441. 与对应负数同时存在的最大正整数 题目介绍 思路 代码 收获 2442. 反转之后不同整数的数目 ...
- LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)
LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...
- LeetCode刷题记录14——257. Binary Tree Paths(easy)
LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...
- LeetCode刷题记录12——232. Implement Queue using Stacks(easy)
LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...
- LeetCode刷题记录11——290. Word Pattern(easy)
LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...
- LeetCode刷题记录10——434. Number of Segments in a String(easy)
LeetCode刷题记录10--434. Number of Segments in a String(easy) 目录 LeetCode刷题记录9--434. Number of Segments ...
- LeetCode刷题记录9——58. Length of Last Word(easy)
LeetCode刷题记录9--58. Length of Last Word(easy) 目录 LeetCode刷题记录9--58. Length of Last Word(easy) 题目 语言 思 ...
- LeetCode刷题记录8——605. Can Place Flowers(easy)
LeetCode刷题记录8--605. Can Place Flowers(easy) 目录 LeetCode刷题记录8--605. Can Place Flowers(easy) 题目 语言 思路 ...
- LeetCode刷题记录7——824. Goat Latin(easy)
LeetCode刷题记录7--824. Goat Latin(easy) 目录 LeetCode刷题记录7--824. Goat Latin(easy) 题目 语言 思路 后记 题目 题目需要将一个输 ...
最新文章
- python脚本设置linux环境变量_Linux环境变量export方法与修改文件方法的区别
- 使用帆软finereport实现跳转的一点心得
- 下边框_OPPO Find X2颜值有多高?超窄下边框+瀑布屏,看完大写的服气
- python 读取 word 表格_python读取word表格
- 云原生数据库_数据标签竞赛云原生地理空间冲刺
- MVVM Light Toolkit使用指南
- 命令行下 pdb 调试 Python 程序
- YOLOv5瓷砖表面瑕疵质检
- ★★停止动画和停止所有动画$(selector).stop() 详解
- fileupload 上传文件函数 --把图片名用日期时间命名
- php怎么改背景图片,php - 如何借助php中的url参数更改背景图片? - SO中文参考 - www.soinside.com...
- 慕课网仿去哪儿项目笔记--(一)-初始化准备
- MATLAB与系统仿真书梅中磊,MatlabSimulink系统建模与仿真.pdf
- 手机刷入面具_【手机改造计划】搞机助手 - 小白刷机必备工具,搞机一条龙:解锁、root、刷面具、刷xposed框架、模块下载安装、自动救砖...
- COMPASS数据上报总结
- edge浏览器登录谷歌账号显示此浏览器或应用可能不安全
- b700a怎么连蓝牙_233621蓝牙21无线音箱B700A小测
- 一个程序员的平凡日常
- LeCun:赋予机器 “常识” ,重新设计神经网络将是AI 研究重点
- Python社区医疗服务平台的设计与实现毕业设计源码221707