当前位置:网站首页>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); */
边栏推荐
- 【C 题集】of Ⅵ
- openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
- Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
- Dynamic layout management
- Implementation of "quick start electronic" window dragging
- 20220601 Mathematics: zero after factorial
- Opencv Harris corner detection
- Octave instructions
- 2.1 Dynamic programming and case study: Jack‘s car rental
- Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
猜你喜欢
LeetCode - 673. 最长递增子序列的个数
LeetCode - 919. 完全二叉树插入器 (数组)
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
MySQL root user needs sudo login
CV learning notes ransca & image similarity comparison hash
Leetcode-112: path sum
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
Leetcode-513: find the lower left corner value of the tree
Mise en œuvre d'OpenCV + dlib pour changer le visage de Mona Lisa
CV learning notes alexnet
随机推荐
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
Leetcode-112: path sum
Problems encountered when MySQL saves CSV files
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
Label Semantic Aware Pre-training for Few-shot Text Classification
LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
Installation and removal of MySQL under Windows
2.2 DP: Value Iteration & Gambler‘s Problem
Leetcode bit operation
Adaptiveavgpool1d internal implementation
20220601数学:阶乘后的零
Leetcode-513: find the lower left corner value of the tree
2. Elment UI date selector formatting problem
Leetcode - 460 LFU cache (Design - hash table + bidirectional linked hash table + balanced binary tree (TreeSet))*
2312. Selling wood blocks | things about the interviewer and crazy Zhang San (leetcode, with mind map + all solutions)
Leetcode-513:找树的左下角值
openCV+dlib实现给蒙娜丽莎换脸
Vgg16 migration learning source code
Toolbutton property settings
QT detection card reader analog keyboard input