当前位置:网站首页>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 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)
- 20220603 Mathematics: pow (x, n)
- CV learning notes ransca & image similarity comparison hash
- 20220609其他:多数元素
- QT self drawing button with bubbles
- . DLL and Differences between lib files
- 3.1 Monte Carlo Methods & case study: Blackjack of on-Policy Evaluation
- 03 fastjason solves circular references
- My openwrt learning notes (V): choice of openwrt development hardware platform - mt7688
- LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
猜你喜欢
Opencv Harris corner detection
LeetCode - 5 最长回文子串
What can I do to exit the current operation and confirm it twice?
Leetcode 300 最长上升子序列
CV learning notes - feature extraction
One click generate traffic password (exaggerated advertisement title)
Leetcode-513: find the lower left corner value of the tree
使用密钥对的形式连接阿里云服务器
LeetCode - 673. 最长递增子序列的个数
Deep learning by Pytorch
随机推荐
Circular queue related design and implementation reference 1
Leetcode-404: sum of left leaves
yocto 技術分享第四期:自定義增加軟件包支持
20220610其他:任务调度器
Vgg16 migration learning source code
Opencv notes 20 PCA
20220609其他:多数元素
Matplotlib drawing
2312. Selling wood blocks | things about the interviewer and crazy Zhang San (leetcode, with mind map + all solutions)
20220608其他:逆波兰表达式求值
4G module designed by charging pile obtains signal strength and quality
Leetcode - 933 number of recent requests
CV learning notes - camera model (Euclidean transformation and affine transformation)
Label Semantic Aware Pre-training for Few-shot Text Classification
Label Semantic Aware Pre-training for Few-shot Text Classification
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
【C 题集】of Ⅵ
LeetCode - 673. Number of longest increasing subsequences
Flutter 退出当前操作二次确认怎么做才更优雅?
Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy