当前位置:网站首页>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); */
边栏推荐
- Gpiof6, 7, 8 configuration
- Education is a pass and ticket. With it, you can step into a higher-level environment
- 学习开发没有捷径,也几乎不存在带路会学的快一些的情况
- (2)接口中新增的方法
- Problems encountered when MySQL saves CSV files
- 51 MCU tmod and timer configuration
- LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
- 2021-10-27
- Simple use of MySQL (addition, deletion, modification and query)
- 2.Elment Ui 日期选择器 格式化问题
猜你喜欢

Yocto Technology Sharing Phase 4: Custom add package support

新系列单片机还延续了STM32产品家族的低电压和节能两大优势

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

嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走

应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机

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

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

YOLO_ V1 summary

Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~

2. Elment UI date selector formatting problem
随机推荐
內存數據庫究竟是如何發揮內存優勢的?
Retinaface: single stage dense face localization in the wild
QT self drawing button with bubbles
Serial communication based on 51 single chip microcomputer
Exception handling of arm
(2) New methods in the interface
学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
(2)接口中新增的方法
Stm32f407 key interrupt
新系列单片机还延续了STM32产品家族的低电压和节能两大优势
03 FastJson 解决循环引用
Toolbutton property settings
Mysql database underlying foundation column
2021-01-03
Drive and control program of Dianchuan charging board for charging pile design
A lottery like scissors, stone and cloth (C language)
Education is a pass and ticket. With it, you can step into a higher-level environment
Runtime. getRuntime(). GC () and runtime getRuntime(). The difference between runfinalization()
03 fastjason solves circular references
2021-10-27