当前位置:网站首页>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); */
边栏推荐
- Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)
- Dictionary tree prefix tree trie
- Yocto Technology Sharing Phase 4: Custom add package support
- 20220603 Mathematics: pow (x, n)
- Opencv note 21 frequency domain filtering
- Leetcode-106: construct a binary tree according to the sequence of middle and later traversal
- Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
- CV learning notes - camera model (Euclidean transformation and affine transformation)
- yocto 技术分享第四期:自定义增加软件包支持
- About windows and layout
猜你喜欢

Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction

Leetcode-112:路径总和

Opencv note 21 frequency domain filtering

Leetcode 300 longest ascending subsequence

LeetCode - 933 最近的请求次数

openCV+dlib實現給蒙娜麗莎換臉

Retinaface: single stage dense face localization in the wild

Matplotlib drawing

YOLO_ V1 summary

2. Elment UI date selector formatting problem
随机推荐
(2) New methods in the interface
3.1 Monte Carlo Methods & case study: Blackjack of on-Policy Evaluation
LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
20220604 Mathematics: square root of X
The data read by pandas is saved to the MySQL database
20220609其他:多数元素
El table X-axis direction (horizontal) scroll bar slides to the right by default
4G module designed by charging pile obtains signal strength and quality
RESNET code details
01 business structure of imitation station B project
20220601 Mathematics: zero after factorial
Flutter 退出当前操作二次确认怎么做才更优雅?
Installation and removal of MySQL under Windows
『快速入门electron』之实现窗口拖拽
CV learning notes - edge extraction
Tensorflow built-in evaluation
Swing transformer details-2