当前位置:网站首页>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); */
边栏推荐
- El table X-axis direction (horizontal) scroll bar slides to the right by default
- Application of 51 single chip microcomputer timer
- Serial communication based on 51 single chip microcomputer
- 自动装箱与拆箱了解吗?原理是什么?
- Gpiof6, 7, 8 configuration
- Pymssql controls SQL for Chinese queries
- Development of intelligent charging pile (I): overview of the overall design of the system
- [combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
- Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~
- STM32 general timer output PWM control steering gear
猜你喜欢
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
Swing transformer details-1
Adaptiveavgpool1d internal implementation
YOLO_ V1 summary
Windows下MySQL的安装和删除
Comment la base de données mémoire joue - t - elle l'avantage de la mémoire?
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
学习开发没有捷径,也几乎不存在带路会学的快一些的情况
单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
Pymssql controls SQL for Chinese queries
随机推荐
我想各位朋友都应该知道学习的基本规律就是:从易到难
SCM is now overwhelming, a wide variety, so that developers are overwhelmed
Application of 51 single chip microcomputer timer
After clicking the Save button, you can only click it once
Swing transformer details-2
学习开发没有捷径,也几乎不存在带路会学的快一些的情况
Synchronization control between tasks
getopt_ Typical use of long function
在三线城市、在县城,很难毕业就拿到10K
Tensorflow built-in evaluation
Getting started with JMX, MBean, mxbean, mbeanserver
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
Positive and negative sample division and architecture understanding in image classification and target detection
01仿B站项目业务架构
A lottery like scissors, stone and cloth (C language)
Google browser plug-in recommendation
Simulate mouse click
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
Qcombox style settings
Which language should I choose to program for single chip microcomputer