当前位置:网站首页>Leetcode - 705 design hash set (Design)
Leetcode - 705 design hash set (Design)
2022-07-03 10:12:00 【Cute at the age of three @d】


Array


class MyHashSet {
private boolean[] set;
public MyHashSet() {
set = new boolean[1000001];
}
public void add(int key) {
set[key] = true;
}
public void remove(int key) {
set[key] = false;
}
public boolean contains(int key) {
return set[key];
}
}
/** * Your MyHashSet object will be instantiated and called as such: * MyHashSet obj = new MyHashSet(); * obj.add(key); * obj.remove(key); * boolean param_3 = obj.contains(key); */
Zipper method


class MyHashSet {
private List<Integer> list[];
private int capacity;
public MyHashSet() {
capacity = 1000;
list = new ArrayList[this.capacity];
}
public int hash(int key){
return key % this.capacity;
}
public void add(int key) {
int h = hash(key);
if(list[h] == null){
list[h] = new ArrayList<>();
list[h].add(key);
}
else{
if(!list[h].contains((Integer) key))
list[h].add(key);
}
}
public void remove(int key) {
int h = hash(key);
if(list[h] != null){
if(list[h].contains((Integer) key))
list[h].remove((Integer) key);
}
}
public boolean contains(int key) {
int h = hash(key);
if(list[h] != null){
if(list[h].contains((Integer) key))
return true;
else
return false;
}
else
return false;
}
}
/** * Your MyHashSet object will be instantiated and called as such: * MyHashSet obj = new MyHashSet(); * obj.add(key); * obj.remove(key); * boolean param_3 = obj.contains(key); */
边栏推荐
- Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
- LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
- 4G module designed by charging pile obtains signal strength and quality
- 2.1 Dynamic programming and case study: Jack‘s car rental
- When the reference is assigned to auto
- One click generate traffic password (exaggerated advertisement title)
- Replace the files under the folder with sed
- YOLO_ V1 summary
- Opencv note 21 frequency domain filtering
- Opencv feature extraction - hog
猜你喜欢

Vgg16 migration learning source code

Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)

LeetCode - 706 设计哈希映射(设计) *

CV learning notes - camera model (Euclidean transformation and affine transformation)

openCV+dlib实现给蒙娜丽莎换脸

openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍

CV learning notes - image filter

03 fastjason solves circular references

Opencv Harris corner detection

LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
随机推荐
CV learning notes - camera model (Euclidean transformation and affine transformation)
Circular queue related design and implementation reference 1
My 4G smart charging pile gateway design and development related articles
getopt_ Typical use of long function
is_ power_ of_ 2 judge whether it is a multiple of 2
My notes on intelligent charging pile development (II): overview of system hardware circuit design
2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
LeetCode - 900. RLE 迭代器
openCV+dlib实现给蒙娜丽莎换脸
01 business structure of imitation station B project
Design of charging pile mqtt transplantation based on 4G EC20 module
El table X-axis direction (horizontal) scroll bar slides to the right by default
Simulate mouse click
CV learning notes - deep learning
20220610其他:任务调度器
20220604 Mathematics: square root of X
Development of intelligent charging pile (I): overview of the overall design of the system
20220606 Mathematics: fraction to decimal
3.1 Monte Carlo Methods & case study: Blackjack of on-Policy Evaluation