当前位置:网站首页>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); */
边栏推荐
- CV learning notes - feature extraction
- Opencv interview guide
- On the problem of reference assignment to reference
- Problems encountered when MySQL saves CSV files
- (1) What is a lambda expression
- 20220604数学:x的平方根
- LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)
- Swing transformer details-1
- YOLO_ V1 summary
- openCV+dlib實現給蒙娜麗莎換臉
猜你喜欢
CV learning notes - BP neural network training example (including detailed calculation process and formula derivation)
Crash工具基本使用及实战分享
Retinaface: single stage dense face localization in the wild
LeetCode - 673. 最长递增子序列的个数
QT is a method of batch modifying the style of a certain type of control after naming the control
Leetcode-106: construct a binary tree according to the sequence of middle and later traversal
Leetcode-106:根据中后序遍历序列构造二叉树
CV learning notes - scale invariant feature transformation (SIFT)
Opencv note 21 frequency domain filtering
LeetCode - 706 设计哈希映射(设计) *
随机推荐
Discrete-event system
Google browser plug-in recommendation
Toolbutton property settings
20220531数学:快乐数
Flutter 退出当前操作二次确认怎么做才更优雅?
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
Yocto technology sharing phase IV: customize and add software package support
03 fastjason solves circular references
Deep learning by Pytorch
Leetcode-112:路径总和
Leetcode 300 longest ascending subsequence
Leetcode-106:根据中后序遍历序列构造二叉树
LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
20220605 Mathematics: divide two numbers
20220609其他:多数元素
Replace the files under the folder with sed
20220604 Mathematics: square root of X
Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)
getopt_ Typical use of long function
Dynamic layout management