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 ...
随机推荐
- Go Example--格式化字符串
package main import ( "fmt" "os" ) type point struct { x, y int } func main() { ...
- LeetCode - Unique Email Addresses
Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...
- python中的is和==
is和== Python中的对象包含三要素:id.type.value id方法的返回值就是对象的内存地址其中id用来唯一标识一个对象,type标识对象的类型,value是对象的值 is 判断的是a对 ...
- 安装包安装npm
在阿里云机器上centos7安装npm可以直接yum安装,然后基于镜像的时候安装不了,直接使用安装包安装,记录一下: 官网下载地址:https://nodejs.org/en/download/ #! ...
- Sublime远程连接Linux
1:Ctrl+Shift+P,输入install 选择红框框然后Enter 2:输入ftp,然后找到sftp,Enter 3:修改配置 修改账号密码信息远程路径之后 ...
- Hanlp分词1.7版本在Spark中分布式使用记录
新发布1.7.0版本的hanlp自然语言处理工具包差不多已经有半年时间了,最近也是一直在整理这个新版本hanlp分词工具的相关内容.不过按照当前的整理进度,还需要一段时间再给大家详细分享整理的内容.昨 ...
- ionic3 热更新发布步骤记录
1.安装基本框架npm install -g ionic@latest npm install -g cordova ionic 验证版本号 ionic –version cordova -versi ...
- <Numerical Analysis>(by Timothy Sauer) Notes
2ed, by Timothy Sauer DEFINITION 1.3A solution is correct within p decimal places if the error is l ...
- linux git pull/push时提示输入账号密码之免除设置
1.先cd到根目录,执行git config --global credential.helper store命令 [root@iZ25mi9h7ayZ ~]# git config --global ...
- Linux Bash Shell字符串分割substring等(转)
原文https://blog.csdn.net/wuyinggui10000/article/details/52779364 脚本开发中遇到的问题是:原java中的字符串分割操作(substring ...