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


超大数组

class MyHashMap {
private int[] map ;
public MyHashMap() {
map = new int[1000001];
Arrays.fill(map,-1);
}
public void put(int key, int value) {
map[key] = value;
}
public int get(int key) {
return map[key];
}
public void remove(int key) {
map[key] = -1;
}
}
拉链法


class MyHashMap {
class Pair{
private int key;
private int value;
public Pair(int key,int value){
this.key = key;
this.value = value;
}
public void setValue(int value){
this.value = value;
}
public int getKey(){
return this.key;
}
public int getValue(){
return this.value;
}
}
private List<Pair> list[];
private int capacity;
public MyHashMap() {
capacity = 1000;
list = new ArrayList[capacity];
}
public int hash(int key){
return key % this.capacity;
}
public void put(int key, int value) {
int h = hash(key);
boolean flag = false;
if(list[h] != null){
for(int i = 0; i < list[h].size();i++){
if(list[h].get(i).getKey() == key){
list[h].get(i).setValue(value);
flag = true;
}
}
if(flag == false)
list[h].add(new Pair(key,value));
}
else{
list[h] = new ArrayList();
list[h].add(new Pair(key,value));
}
}
public int get(int key) {
int h = hash(key);
if(list[h] != null){
for(int i = 0; i < list[h].size();i++){
if(list[h].get(i).getKey() == key)
return list[h].get(i).getValue();
}
}
return -1;
}
public void remove(int key) {
int h = hash(key);
Pair pair = null;
if(list[h] != null){
for(int i = 0; i < list[h].size();i++){
if(list[h].get(i).getKey() == key){
pair = list[h].get(i);
break;
}
}
}
if(pair != null)
list[h].remove(pair);
}
}
/** * Your MyHashMap object will be instantiated and called as such: * MyHashMap obj = new MyHashMap(); * obj.put(key,value); * int param_2 = obj.get(key); * obj.remove(key); */
边栏推荐
- [keil5 debugging] warning:enumerated type mixed with other type
- CEF download, compile project
- The data read by pandas is saved to the MySQL database
- 01 business structure of imitation station B project
- Swing transformer details-1
- Liquid crystal display
- On the problem of reference assignment to reference
- Not many people can finally bring their interests to college graduation
- Stm32 NVIC interrupt priority management
- (1) What is a lambda expression
猜你喜欢
![[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer](/img/90/4de927e797ec9c2bb70e507392bed0.jpg)
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer

There is no specific definition of embedded system

JS foundation - prototype prototype chain and macro task / micro task / event mechanism

openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
![[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)](/img/19/5dc152b3fadeb56de50768561ad659.jpg)
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)

Working mode of 80C51 Serial Port

LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*

QT is a method of batch modifying the style of a certain type of control after naming the control

Gpiof6, 7, 8 configuration

Notes on C language learning of migrant workers majoring in electronic information engineering
随机推荐
在三线城市、在县城,很难毕业就拿到10K
STM32 running lantern experiment - library function version
Development of intelligent charging pile (I): overview of the overall design of the system
使用密钥对的形式连接阿里云服务器
It is difficult to quantify the extent to which a single-chip computer can find a job
Adaptiveavgpool1d internal implementation
Basic knowledge of communication interface
MySQL的简单使用(增删改查)
There is no specific definition of embedded system
QT detection card reader analog keyboard input
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
Positive and negative sample division and architecture understanding in image classification and target detection
要选择那种语言为单片机编写程序呢
Swing transformer details-2
自动装箱与拆箱了解吗?原理是什么?
03 fastjason solves circular references
单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了
51 MCU tmod and timer configuration
Stm32f04 clock configuration
(2) New methods in the interface