当前位置:网站首页>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); */
边栏推荐
- Circular queue related design and implementation reference 1
- openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
- 4G module at command communication package interface designed by charging pile
- 2020-08-23
- 一个可执行的二进制文件包含的不仅仅是机器指令
- Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
- Problems encountered when MySQL saves CSV files
- Basic knowledge of MySQL database (an introduction to systematization)
- el-table X轴方向(横向)滚动条默认滑到右边
- 2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
猜你喜欢

Interruption system of 51 single chip microcomputer

03 FastJson 解决循环引用

Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving

Crash工具基本使用及实战分享

Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program

当你需要使用STM32某些功能,而51实现不了时, 那32自然不需要学

Windows下MySQL的安装和删除

03 fastjason solves circular references

My notes on the development of intelligent charging pile (III): overview of the overall design of the system software

应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机
随机推荐
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
A lottery like scissors, stone and cloth (C language)
Drive and control program of Dianchuan charging board for charging pile design
Interruption system of 51 single chip microcomputer
51 MCU tmod and timer configuration
Dynamic layout management
YOLO_ V1 summary
2021-10-27
编程思想比任何都重要,不是比谁多会用几个函数而是比程序的理解
Adaptiveavgpool1d internal implementation
(2) New methods in the interface
內存數據庫究竟是如何發揮內存優勢的?
Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
[keil5 debugging] warning:enumerated type mixed with other type
2020-08-23
MySQL 数据库基础知识(系统化一篇入门)
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
Wireshark use
is_ power_ of_ 2 judge whether it is a multiple of 2
MySQL root user needs sudo login