当前位置:网站首页>LeetCode - 705 设计哈希集合(设计)
LeetCode - 705 设计哈希集合(设计)
2022-07-03 09:20:00 【三岁就很萌@D】


数组


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); */
拉链法


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); */
边栏推荐
- Application of 51 single chip microcomputer timer
- 01 business structure of imitation station B project
- YOLO_ V1 summary
- 新系列单片机还延续了STM32产品家族的低电压和节能两大优势
- Adaptiveavgpool1d internal implementation
- Swing transformer details-2
- Drive and control program of Dianchuan charging board for charging pile design
- There is no specific definition of embedded system
- Design of charging pile mqtt transplantation based on 4G EC20 module
- getopt_ Typical use of long function
猜你喜欢

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

没有多少人能够最终把自己的兴趣带到大学毕业上

STM32 interrupt switch

编程思想比任何都重要,不是比谁多会用几个函数而是比程序的理解

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

Stm32f407 key interrupt

How does the memory database give full play to the advantages of memory?

Runtime. getRuntime(). GC () and runtime getRuntime(). The difference between runfinalization()

ADS simulation design of class AB RF power amplifier

Crash工具基本使用及实战分享
随机推荐
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
I didn't think so much when I was in the field of single chip microcomputer. I just wanted to earn money to support myself first
Yocto Technology Sharing Phase 4: Custom add package support
新系列单片机还延续了STM32产品家族的低电压和节能两大优势
Interruption system of 51 single chip microcomputer
学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
当你需要使用STM32某些功能,而51实现不了时, 那32自然不需要学
2020-08-23
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
2. Elment UI date selector formatting problem
STM32 interrupt switch
Crash工具基本使用及实战分享
Notes on C language learning of migrant workers majoring in electronic information engineering
要選擇那種語言為單片機編寫程序呢
Vscode markdown export PDF error
After clicking the Save button, you can only click it once
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
Pymssql controls SQL for Chinese queries
Retinaface: single stage dense face localization in the wild
Vgg16 migration learning source code