当前位置:网站首页>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); */
边栏推荐
- Emballage automatique et déballage compris? Quel est le principe?
- QT is a method of batch modifying the style of a certain type of control after naming the control
- QT detection card reader analog keyboard input
- Stm32f04 clock configuration
- QT setting suspension button
- Yocto technology sharing phase IV: customize and add software package support
- 2020-08-23
- 自动装箱与拆箱了解吗?原理是什么?
- Application of external interrupts
- 单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了
猜你喜欢
JS基础-原型原型链和宏任务/微任务/事件机制
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
Working mode of 80C51 Serial Port
There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
Oracle database SQL statement execution plan, statement tracking and optimization instance
Interruption system of 51 single chip microcomputer
干单片机这一行的时候根本没想过这么多,只想着先挣钱养活自己
CEF download, compile project
嵌入式系统没有特别明确的定义
03 FastJson 解决循环引用
随机推荐
After clicking the Save button, you can only click it once
Drive and control program of Dianchuan charging board for charging pile design
03 fastjason solves circular references
Oracle database SQL statement execution plan, statement tracking and optimization instance
Interruption system of 51 single chip microcomputer
Serial port programming
Fundamentals of Electronic Technology (III)_ Chapter 2 principle of amplification circuit__ Crystal triode and field effect triode
Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
Synchronization control between tasks
51 MCU tmod and timer configuration
YOLO_ V1 summary
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
Getting started with JMX, MBean, mxbean, mbeanserver
单片机学到什么程度能找到工作,这个标准不好量化
QT self drawing button with bubbles
Vscode markdown export PDF error
The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network
STM32 interrupt switch
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)