当前位置:网站首页>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); */
边栏推荐
- Window maximum and minimum settings
- 2312. Selling wood blocks | things about the interviewer and crazy Zhang San (leetcode, with mind map + all solutions)
- Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
- My openwrt learning notes (V): choice of openwrt development hardware platform - mt7688
- My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
- Cases of OpenCV image enhancement
- After clicking the Save button, you can only click it once
- Discrete-event system
- Development of intelligent charging pile (I): overview of the overall design of the system
- Label Semantic Aware Pre-training for Few-shot Text Classification
猜你喜欢

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

What can I do to exit the current operation and confirm it twice?

Retinaface: single stage dense face localization in the wild

Crash工具基本使用及实战分享

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

My notes on intelligent charging pile development (II): overview of system hardware circuit design

1. Finite Markov Decision Process

3.3 Monte Carlo Methods: case study: Blackjack of Policy Improvement of on- & off-policy Evaluation

Opencv+dlib to change the face of Mona Lisa

LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
随机推荐
Anaconda安装包 报错packagesNotFoundError: The following packages are not available from current channels:
CV learning notes - feature extraction
RESNET code details
Window maximum and minimum settings
CV learning notes - deep learning
Opencv+dlib to change the face of Mona Lisa
Leetcode-112:路径总和
Opencv notes 20 PCA
CV learning notes - clustering
2312. Selling wood blocks | things about the interviewer and crazy Zhang San (leetcode, with mind map + all solutions)
Label Semantic Aware Pre-training for Few-shot Text Classification
Development of intelligent charging pile (I): overview of the overall design of the system
YOLO_ V1 summary
QT self drawing button with bubbles
2. Elment UI date selector formatting problem
Crash工具基本使用及实战分享
LeetCode - 900. RLE 迭代器
Octave instructions
2021-11-11 standard thread library
LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))