当前位置:网站首页>Leetcode - 706 design hash mapping (Design)*
Leetcode - 706 design hash mapping (Design)*
2022-07-03 10:12:00 【Cute at the age of three @d】
Super large array
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;
}
}
Zipper method
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); */
边栏推荐
- 20220606 Mathematics: fraction to decimal
- Opencv image rotation
- getopt_ Typical use of long function
- Opencv histogram equalization
- 20220604 Mathematics: square root of X
- QT detection card reader analog keyboard input
- Yocto Technology Sharing Phase 4: Custom add package support
- 20220531数学:快乐数
- LeetCode - 705 设计哈希集合(设计)
- 20220603 Mathematics: pow (x, n)
猜你喜欢
yocto 技術分享第四期:自定義增加軟件包支持
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
Pycharm cannot import custom package
Adaptiveavgpool1d internal implementation
Cases of OpenCV image enhancement
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
openCV+dlib实现给蒙娜丽莎换脸
pycharm 无法引入自定义包
LeetCode - 933 最近的请求次数
Opencv+dlib to change the face of Mona Lisa
随机推荐
QT is a method of batch modifying the style of a certain type of control after naming the control
Mise en œuvre d'OpenCV + dlib pour changer le visage de Mona Lisa
4.1 Temporal Differential of one step
LeetCode - 5 最长回文子串
20220603 Mathematics: pow (x, n)
2021-11-11 standard thread library
20220610其他:任务调度器
The underlying principle of vector
MySQL root user needs sudo login
Leetcode-513: find the lower left corner value of the tree
CV learning notes - reasoning and training
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
openCV+dlib實現給蒙娜麗莎換臉
Circular queue related design and implementation reference 1
QT setting suspension button
Leetcode - 460 LFU cache (Design - hash table + bidirectional linked hash table + balanced binary tree (TreeSet))*
When the reference is assigned to auto
Tensorflow built-in evaluation
RESNET code details
20220601数学:阶乘后的零