当前位置:网站首页>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); */
边栏推荐
- Screen display of charging pile design -- led driver ta6932
- [untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
- 4G module board level control interface designed by charging pile
- Windows下MySQL的安装和删除
- Which language should I choose to program for single chip microcomputer
- 没有多少人能够最终把自己的兴趣带到大学毕业上
- ADS simulation design of class AB RF power amplifier
- 在三线城市、在县城,很难毕业就拿到10K
- YOLO_ V1 summary
- [keil5 debugging] warning:enumerated type mixed with other type
猜你喜欢

An executable binary file contains more than machine instructions

Comment la base de données mémoire joue - t - elle l'avantage de la mémoire?

內存數據庫究竟是如何發揮內存優勢的?

Fundamentals of Electronic Technology (III)_ Chapter 2 principle of amplification circuit__ Crystal triode and field effect triode

对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门

El table X-axis direction (horizontal) scroll bar slides to the right by default

How does the memory database give full play to the advantages of memory?

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

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

Oracle database SQL statement execution plan, statement tracking and optimization instance
随机推荐
Windows下MySQL的安装和删除
Emballage automatique et déballage compris? Quel est le principe?
QT setting suspension button
is_ power_ of_ 2 judge whether it is a multiple of 2
Swing transformer details-1
Simulate mouse click
Synchronization control between tasks
Education is a pass and ticket. With it, you can step into a higher-level environment
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
yocto 技術分享第四期:自定義增加軟件包支持
03 fastjason solves circular references
A lottery like scissors, stone and cloth (C language)
要選擇那種語言為單片機編寫程序呢
Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~
(1) 什么是Lambda表达式
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
QT is a method of batch modifying the style of a certain type of control after naming the control
Oracle database SQL statement execution plan, statement tracking and optimization instance
使用sed替换文件夹下文件