当前位置:网站首页>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); */
边栏推荐
- 20220606数学:分数到小数
- 20220601数学:阶乘后的零
- 2.1 Dynamic programming and case study: Jack‘s car rental
- 20220531数学:快乐数
- Tensorflow built-in evaluation
- Label Semantic Aware Pre-training for Few-shot Text Classification
- Opencv notes 17 template matching
- LeetCode - 673. Number of longest increasing subsequences
- CV learning notes alexnet
- LeetCode - 5 最长回文子串
猜你喜欢

Opencv+dlib to change the face of Mona Lisa

LeetCode - 919. 完全二叉树插入器 (数组)

Mise en œuvre d'OpenCV + dlib pour changer le visage de Mona Lisa

Discrete-event system

ADS simulation design of class AB RF power amplifier

CV learning notes - clustering

03 fastjason solves circular references

One click generate traffic password (exaggerated advertisement title)

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 - 919. Full binary tree inserter (array)
LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
is_ power_ of_ 2 judge whether it is a multiple of 2
01 business structure of imitation station B project
Problems encountered when MySQL saves CSV files
Leetcode 300 最长上升子序列
Cases of OpenCV image enhancement
Opencv feature extraction sift
Wireshark use
4.1 Temporal Differential of one step
. DLL and Differences between lib files
LeetCode - 673. Number of longest increasing subsequences
Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)
2312. Selling wood blocks | things about the interviewer and crazy Zhang San (leetcode, with mind map + all solutions)
Dictionary tree prefix tree trie
QT is a method of batch modifying the style of a certain type of control after naming the control
Leetcode - 933 number of recent requests
LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)