当前位置:网站首页>golang刷leetcode 经典(6) 实现跳表
golang刷leetcode 经典(6) 实现跳表
2022-08-02 17:37:00 【用户9710217】
不使用任何内建的哈希表库设计一个哈希映射
具体地说,你的设计应该包含以下的功能
put(key, value):向哈希映射中插入(键,值)的数值对。如果键对应的值已经存在,更新这个值。
get(key):返回给定的键所对应的值,如果映射中不包含这个键,返回-1。
remove(key):如果映射中存在这个键,删除这个数值对。
示例:
MyHashMap hashMap = new MyHashMap();
hashMap.put(1, 1);
hashMap.put(2, 2);
hashMap.get(1); // 返回 1
hashMap.get(3); // 返回 -1 (未找到)
hashMap.put(2, 1); // 更新已有的值
hashMap.get(2); // 返回 1
hashMap.remove(2); // 删除键为2的数据
hashMap.get(2); // 返回 -1 (未找到)
注意:
所有的值都在 [1, 1000000]的范围内。
操作的总数目在[1, 10000]范围内。
不要使用内建的哈希库。
解题思路:
1,使用拉链法
2,和hashset区别是节点里存的内容
A,hashset 里存key
B,hashmap里存key,value
type MyHashMap struct {
data []*Node
len int
}
type Node struct{
key int
value int
next *Node
}
/** Initialize your data structure here. */
func Constructor() MyHashMap {
return MyHashMap{
data:make([]*Node,10001),
len:10001,
}
}
/** value will always be non-negative. */
func (this *MyHashMap) Put(key int, value int) {
data:=this.data[key%this.len]
if data==nil{
this.data[key%this.len]=&Node{
key:key,
value:value,
}
}else{
if data.key==key{
data.value=value
return
}
for data.next!=nil{
data=data.next
if data.key==key{
data.value=value
return
}
}
data.next=&Node{
key:key,
value:value,
}
}
//this.Print()
}
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
func (this *MyHashMap) Get(key int) int {
data:=this.data[key%this.len]
if data==nil{
return -1
}
for data.next!=nil{
if data.key==key{
return data.value
}
data=data.next
}
if data.key==key {
return data.value
}
return -1
}
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
func (this *MyHashMap) Remove(key int) {
data:=this.data[key%this.len]
if data==nil{
return
}
if data.key==key{
this.data[key%this.len]=data.next
//this.Print()
return
}
for data.next!=nil{
if data.next.key!=key{
data=data.next
}else{
data.next=data.next.next
}
}
}
func (this *MyHashMap)Print(){
fmt.Println("----",this.len,":")
for i:=0;i<len(this.data);i++{
d:=this.data[i]
if d!=nil{
fmt.Println(this.data[i].key)
for d.next!=nil{
d=d.next
fmt.Println(d.key)
}
}
}
fmt.Println(":------")
}
/**
* Your MyHashMap object will be instantiated and called as such:
* obj := Constructor();
* obj.Put(key,value);
* param_2 := obj.Get(key);
* obj.Remove(key);
*/边栏推荐
猜你喜欢
随机推荐
docker安装Oracle之后常用的一些命令
Mini Program Graduation Works WeChat Gymnasium Reservation Mini Program Graduation Design Finished Product (8) Graduation Design Thesis Template
如何生成随机数+原理详细分析
FP6606CLP5 SOP-8 USB Type-C和PD充电控制器
golang源码分析(10)slice
再获权威认证!马上消费安逸花APP通过中国信通院“金融APP人脸识别安全能力评测”
Smart Microelectronics Releases Low-Power MM32L0130 Series MCU Products
NeRF: The Secret of 3D Reconstruction Technology in the Popular Scientific Research Circle
Ubuntu系统下用docker安装oracle
vulnhub W34kn3ss: 1
Go 语言快速入门指南:第二篇 变量与常量
JS数组删除其中一个元素
KunlunBase 1.0 发布了!
恒驰5真的没大卖
阿波罗 planning代码-modules\planning\lattice\trajectory_generation\PiecewiseBrakingTrajectoryGenerator类详解
年轻人接棒大妈,金价跌回“4字头”,七夕迎黄金消费小热潮
莱斯大学胡侠团队 ICML 2022 杰出论文: 新型图数据增强方法 G-Mixup|附作者对话
安全至上:落地DevSecOps最佳实践你不得不知道的工具
LeetCode·每日一题·
打补丁的日子,比写代码的日子难熬多了









