设计hash集合、hash映射
题目1
lettcode-705题
难度:简单
解题思路
既然说是一个HashSet,所以他就是一个不可重复的集合,就意味着在该集合中每个元素只能出现一次。
所以说我们就可以用boolean类型的数组来模拟出这样的一个集合
数组的索引就对应了set的key,数组所对应的布尔值就是set的value,当其为true1时就说明存在,否则就是不存在
具体实现方式如下
具体代码
1 | class MyHashSet { |
题目2
lettcode-706题
难度:简单
解题思路
从题目中我们可以看出来跟上一题差不多,无非是一个set一个map的差别
阅读题目要求我们可以知道对应的key
和value
都是int
类型的,所以我们可以定义一个int类型的数组来存储数据
取值的时候如果取值为空就返回一个-1
,所以我做了一个判断,判断是否有值
具体实现方式如下
具体代码
1 | class MyHashMap { |