当前位置:网站首页>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); */
边栏推荐
- openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
- 2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
- Vgg16 migration learning source code
- Opencv image rotation
- On the problem of reference assignment to reference
- (1) What is a lambda expression
- Replace the files under the folder with sed
- LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
- 使用sed替换文件夹下文件
- The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network
猜你喜欢

LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)

Opencv notes 17 template matching

Yocto technology sharing phase IV: customize and add software package support

QT is a method of batch modifying the style of a certain type of control after naming the control

ADS simulation design of class AB RF power amplifier

Opencv gray histogram, histogram specification

CV learning notes alexnet

El table X-axis direction (horizontal) scroll bar slides to the right by default

Leetcode 300 最长上升子序列

3.1 Monte Carlo Methods & case study: Blackjack of on-Policy Evaluation
随机推荐
CV learning notes alexnet
使用密钥对的形式连接阿里云服务器
Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
Leetcode 300 最长上升子序列
Wireshark use
4G module at command communication package interface designed by charging pile
Label Semantic Aware Pre-training for Few-shot Text Classification
Opencv note 21 frequency domain filtering
openCV+dlib实现给蒙娜丽莎换脸
01 business structure of imitation station B project
2021-11-11 standard thread library
3.1 Monte Carlo Methods & case study: Blackjack of on-Policy Evaluation
20220604数学:x的平方根
getopt_ Typical use of long function
What can I do to exit the current operation and confirm it twice?
20220605数学:两数相除
20220603数学:Pow(x,n)
Leetcode-106: construct a binary tree according to the sequence of middle and later traversal
Installation and removal of MySQL under Windows
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍