当前位置:网站首页>LeetCode - 706 设计哈希映射(设计) *
LeetCode - 706 设计哈希映射(设计) *
2022-07-03 09:20:00 【三岁就很萌@D】
超大数组
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;
}
}
拉链法
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); */
边栏推荐
- yocto 技术分享第四期:自定义增加软件包支持
- Stm32f04 clock configuration
- MySQL 数据库基础知识(系统化一篇入门)
- 2.Elment Ui 日期选择器 格式化问题
- el-table X轴方向(横向)滚动条默认滑到右边
- 在三线城市、在县城,很难毕业就拿到10K
- Stm32 NVIC interrupt priority management
- QT detection card reader analog keyboard input
- (2) New methods in the interface
- JS foundation - prototype prototype chain and macro task / micro task / event mechanism
猜你喜欢
对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
Comment la base de données mémoire joue - t - elle l'avantage de la mémoire?
An executable binary file contains more than machine instructions
YOLO_ V1 summary
学习开发没有捷径,也几乎不存在带路会学的快一些的情况
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
Blue Bridge Cup for migrant workers majoring in electronic information engineering
Serial communication based on 51 single chip microcomputer
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
Pymssql controls SQL for Chinese queries
随机推荐
You need to use MySQL in the opening experiment. How can you forget the basic select statement? Remedy is coming~
2020-08-23
01 business structure of imitation station B project
Synchronization control between tasks
Project scope management__ Scope management plan and scope specification
el-table X轴方向(横向)滚动条默认滑到右边
4G module designed by charging pile obtains signal strength and quality
单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
Notes on C language learning of migrant workers majoring in electronic information engineering
Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving
Fundamentals of Electronic Technology (III)_ Chapter 2 principle of amplification circuit__ Crystal triode and field effect triode
03 fastjason solves circular references
Vgg16 migration learning source code
Vscode markdown export PDF error
Basic knowledge of communication interface
Swing transformer details-1
LeetCode - 673. 最长递增子序列的个数
4G module IMEI of charging pile design
(2)接口中新增的方法
STM32 general timer 1s delay to realize LED flashing