当前位置:网站首页>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); */
边栏推荐
- Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
- LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
- Opencv feature extraction - hog
- openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
- CV learning notes convolutional neural network
- Circular queue related design and implementation reference 1
- Opencv interview guide
- Leetcode-513:找树的左下角值
- About windows and layout
- 20220531 Mathematics: Happy numbers
猜你喜欢
Flutter 退出当前操作二次确认怎么做才更优雅?
openCV+dlib实现给蒙娜丽莎换脸
Opencv+dlib to change the face of Mona Lisa
Leetcode-112:路径总和
QT self drawing button with bubbles
Pycharm cannot import custom package
CV learning notes - Stereo Vision (point cloud model, spin image, 3D reconstruction)
Opencv notes 17 template matching
LeetCode - 673. Number of longest increasing subsequences
Yocto technology sharing phase IV: customize and add software package support
随机推荐
On the problem of reference assignment to reference
RESNET code details
Window maximum and minimum settings
03 fastjason solves circular references
Deep learning by Pytorch
[C question set] of Ⅵ
Opencv histogram equalization
Leetcode - 460 LFU cache (Design - hash table + bidirectional linked hash table + balanced binary tree (TreeSet))*
Leetcode-112:路径总和
20220601 Mathematics: zero after factorial
Label Semantic Aware Pre-training for Few-shot Text Classification
Design of charging pile mqtt transplantation based on 4G EC20 module
Basic use and actual combat sharing of crash tool
Retinaface: single stage dense face localization in the wild
20220607其他:两整数之和
4G module board level control interface designed by charging pile
Adaptiveavgpool1d internal implementation
QT setting suspension button
Flutter 退出当前操作二次确认怎么做才更优雅?
Discrete-event system