706. Design HashMap 实现哈希表
[抄题]:
public MyHashMap() {
主函数里面是装非final变量的,如果没有,可以一个字都不写
}
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
put: 不论如何都要放。所以如果没有bucket,就要new一个对象出来:
if (buckets[i] == null)
buckets[i] = new Bucket();
get:没有就返回-1
remove:没有就直接return
[思维问题]:
[英文数据结构或算法,为什么不用别的数据结构或算法]:
Bucket类都用final变量,表示容量恒定不变
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
找上一个的bucket[index] ,根据上一个来找下一个:

[一刷]:
- ListNode类中包括ListNode方法
- find函数也是返回前一个prev节点
[二刷]:
- find函数查找的是key,不是value
[三刷]:
- 后面三个函数都要调用find函数啊,不然find干嘛的
- 都要考虑prev.next不存在的情况
[四刷]:
- bucket是一个链表,find(Bucket bucket, int key)要在该链表中查找,而不是数组中
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
Bucket是一个链表,根据上一个节点来找下一个
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
class MyHashMap {
/** Initialize your data structure here. */
public MyHashMap() {
}
//ini: class ListNode, class bucket
class ListNode {
int key, val;
ListNode next;
ListNode(int key, int val) {
this.key = key;
this.val = val;
}
}
class Bucket {
final ListNode head = new ListNode(-1, -1);
}
final Bucket[] buckets = new Bucket[10000000];
//method find, shuld function just as the normal find
ListNode find(Bucket buckets, int key) {
ListNode prev = null; ListNode cur = buckets.head;
//while loop
while (cur != null && cur.key != key) {
prev = cur;
cur = cur.next;
}
return prev;
}
//method getIdx
int getIdx(int key) {
return Integer.hashCode(key) % buckets.length;
}
/** value will always be non-negative. */
public void put(int key, int value) {
int i = getIdx(key);
//cc
if (buckets[i] == null)
buckets[i] = new Bucket();
ListNode prev = find(buckets[i], key);
//the bucket's next is null or not
if (prev.next == null) {
prev.next = new ListNode(key, value);
}else {
prev.next.val = value;
}
}
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
public int get(int key) {
int i = getIdx(key);
//cc
if (buckets[i] == null) return -1;
ListNode prev = find(buckets[i], key);
return prev.next == null ? -1 : prev.next.val;
}
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
public void remove(int key) {
int i = getIdx(key);
//cc
if (buckets[i] == null) return ;
ListNode prev = find(buckets[i], key);
if (prev.next == null) return;
prev.next = prev.next.next;
}
}
/**
* 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);
*/
[潜台词] :
706. Design HashMap 实现哈希表的更多相关文章
- LeetCode 706. Design HashMap (设计哈希映射)
题目标签:HashMap 题目让我们设计一个 hashmap, 有put, get, remove 功能. 建立一个 int array, index 是key, 值是 value,具体看code. ...
- 706. Design HashMap - LeetCode
Question 706. Design HashMap Solution 题目大意:构造一个hashmap 思路:讨个巧,只要求key是int,哈希函数选择f(x)=x,规定key最大为100000 ...
- 【Leetcode_easy】706. Design HashMap
problem 706. Design HashMap solution1: class MyHashMap { public: /** Initialize your data structure ...
- Leetcode PHP题解--D75 706. Design HashMap
2019独角兽企业重金招聘Python工程师标准>>> D75 706. Design HashMap 题目链接 706. Design HashMap 题目分析 自行设计一个has ...
- HashMap分析 + 哈希表
http://www.cnblogs.com/hzmark/archive/2012/12/24/HashMap.html http://www.cnblogs.com/xqzt/archive/20 ...
- Leetcode706.Design HashMap设计哈希映射
不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get(key ...
- [leetcode] 706. Design HashMap
题目 Design a HashMap without using any built-in hash table libraries. Implement the MyHashMap class: ...
- LeetCode 706 Design HashMap 解题报告
题目要求 Design a HashMap without using any built-in hash table libraries. To be specific, your design s ...
- [LeetCode&Python] Problem 706. Design HashMap
Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...
随机推荐
- LG1912 [NOI2009]诗人小G
题意 题目描述 小G是一个出色的诗人,经常作诗自娱自乐.但是,他一直被一件事情所困扰,那就是诗的排版问题. 一首诗包含了若干个句子,对于一些连续的短句,可以将它们用空格隔开并放在一行中,注意一行中可以 ...
- Python数据存储:pickle模块的使用讲解
在机器学习中,我们常常需要把训练好的模型存储起来,这样在进行决策时直接将模型读出,而不需要重新训练模型,这样就大大节约了时间.Python提供的pickle模块就很好地解决了这个问题,它可以序列化对象 ...
- linux查看进程启动的时间点
ps -ef |grep xxx # 先查找进程pid ps -wo pid,lstart -p {pid}
- laravel 路由分組
laravel 路由分組 Route::group(['prefix' => 'admin'], function () { $namespacePrefix="\\App\\Http ...
- 【java】接口
class :用于定义类interface:用于定于接口 接口定义时,特点:1.接口中常见定义:常亮和抽象方法2.接口中的成员都有固定修饰符(如果没有会被隐式添加) 常量:public static ...
- 5G投资逻辑
5G投资逻辑 关注光模块生产厂商. 通信射频滤波器,功率放大器生产厂商. 光无源器件的需求增多
- dll文件32位64位检测工具以及Windows文件夹SysWow64的坑【转发】
原文地址:http://www.cnblogs.com/hbccdf/archive/2014/03/09/3590916.html 自从操作系统升级到64位以后,就要不断的需要面对32位.64位的问 ...
- Power Designer 转C#实体类方法
1.打开Power Designer菜单 Tools,选择如图 2.弹出方框中选择PD安装目录下的如图地址 3.object language选择正确目录后,可选如图语言,如C#.再填写name ...
- vue1 & vue2 数据驱动更新视图机制对比
vue1 小粒度更新,精确追踪到数据变化所影响的dom变化,精确更新变化的dom 具体实现为,维护 observer watcher directive 三个类 ·observer负责监听数据变化,并 ...
- percona-toolki安装冲突(my.cnf Percona-Server-shared与mysql-community-server)
最近在安装percona-toolkit工具包时,提示在my.cnf文件中, Percona-Server-shared与mysql-community-server冲突.起初还以为是一定需安装Per ...