当前位置:网站首页>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 - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*
- Dictionary tree prefix tree trie
- [combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
- Basic use and actual combat sharing of crash tool
- Opencv interview guide
- Leetcode-112:路径总和
- Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)
- CV learning notes - deep learning
- Connect Alibaba cloud servers in the form of key pairs
- 1. Finite Markov Decision Process
猜你喜欢

LeetCode - 933 最近的请求次数

Anaconda安装包 报错packagesNotFoundError: The following packages are not available from current channels:

3.2 Off-Policy Monte Carlo Methods & case study: Blackjack of off-Policy Evaluation

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

openCV+dlib實現給蒙娜麗莎換臉

Deep Reinforcement learning with PyTorch

2.2 DP: Value Iteration & Gambler‘s Problem

Label Semantic Aware Pre-training for Few-shot Text Classification

Vgg16 migration learning source code

Installation and removal of MySQL under Windows
随机推荐
Swing transformer details-2
波士顿房价预测(TensorFlow2.9实践)
Opencv note 21 frequency domain filtering
Mise en œuvre d'OpenCV + dlib pour changer le visage de Mona Lisa
CV learning notes - BP neural network training example (including detailed calculation process and formula derivation)
My openwrt learning notes (V): choice of openwrt development hardware platform - mt7688
Wireshark use
Pycharm cannot import custom package
QT setting suspension button
3.2 Off-Policy Monte Carlo Methods & case study: Blackjack of off-Policy Evaluation
Leetcode - 933 number of recent requests
Leetcode-100:相同的树
Opencv+dlib to change the face of Mona Lisa
Problems encountered when MySQL saves CSV files
CV learning notes - camera model (Euclidean transformation and affine transformation)
CV learning notes - Stereo Vision (point cloud model, spin image, 3D reconstruction)
Leetcode-513:找树的左下角值
Qcombox style settings
QT self drawing button with bubbles
QT detection card reader analog keyboard input