当前位置:网站首页>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); */
边栏推荐
- 4G module IMEI of charging pile design
- Opencv Harris corner detection
- LeetCode - 673. 最长递增子序列的个数
- On the problem of reference assignment to reference
- Replace the files under the folder with sed
- Opencv notes 17 template matching
- Window maximum and minimum settings
- 20220607 others: sum of two integers
- Screen display of charging pile design -- led driver ta6932
- LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
猜你喜欢

3.3 Monte Carlo Methods: case study: Blackjack of Policy Improvement of on- & off-policy Evaluation

yocto 技术分享第四期:自定义增加软件包支持

Opencv note 21 frequency domain filtering

Leetcode - 933 number of recent requests

Flutter 退出当前操作二次确认怎么做才更优雅?

Crash工具基本使用及实战分享

Opencv feature extraction - hog

LeetCode - 673. 最长递增子序列的个数

RESNET code details

2. Elment UI date selector formatting problem
随机推荐
QT setting suspension button
LeetCode - 919. 完全二叉树插入器 (数组)
使用sed替换文件夹下文件
Discrete-event system
CV learning notes - deep learning
Vscode markdown export PDF error
Vgg16 migration learning source code
20220605数学:两数相除
Flutter 退出当前操作二次确认怎么做才更优雅?
Positive and negative sample division and architecture understanding in image classification and target detection
LeetCode - 705 设计哈希集合(设计)
20220601 Mathematics: zero after factorial
Opencv feature extraction sift
Opencv note 21 frequency domain filtering
Leetcode-513: find the lower left corner value of the tree
The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network
Leetcode-513:找树的左下角值
20220607其他:两整数之和
4G module designed by charging pile obtains signal strength and quality
MySQL root user needs sudo login