当前位置:网站首页>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);
*/边栏推荐
- Mini Program Graduation Works WeChat Gymnasium Reservation Mini Program Graduation Design Finished Product (8) Graduation Design Thesis Template
- erp系统和wms系统有什么区别
- golang源码分析(8):m、p、g、shedt、sudog
- Redis总结_实战篇
- 二叉查找树的查找
- Wechat Gymnasium Appointment Mini Program Graduation Design Finished Works Mini Program Graduation Design Finished Work (6) Question Opening Reply PPT
- Navicat 连接Oracle时提示oracle library is not loaded的问题解决
- 查看数据库数据量大小,占用磁盘大小
- 年轻人接棒大妈,金价跌回“4字头”,七夕迎黄金消费小热潮
- 恒驰5真的没大卖
猜你喜欢
随机推荐
HDF驱动框架的API(3)
php弱类型-攻防世界lottery
9月起中国给予多哥等16国98%税目产品零关税待遇
Mysql和Redis如何保证数据一致性
Cpolar application example of data acquisition equipment
二叉查找树的查找
载20(S)-人参皂苷/细胞穿膜肽-单克隆抗体-载丝裂霉素白蛋白纳米微球的制备
golang学习之七:并发编程基础(goroutine、channel、select)
蔚来杯2022牛客暑期多校训练营5 ABCDFGHK
方法的使用
全面认识二极管,一篇文章就够了
How Tencent architects explained: The principle of Redis high-performance communication (essential version)
vulnhub W34kn3ss: 1
「全球数字经济大会」登陆 N 世界,融云提供通信云服务支持
FP6606CLP5 SOP-8 USB Type-C和PD充电控制器
Go 语言快速入门指南:第二篇 变量与常量
CUDA+Pycharm-gpu版本+Anaconda安装
Several common cross-domain solutions
谁抢走了华大基因的生意?
ES: export 的用法









