[抄题]:

public MyHashMap() {
 主函数里面是装非final变量的,如果没有,可以一个字都不写
}

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

put: 不论如何都要放。所以如果没有bucket,就要new一个对象出来:

if (buckets[i] == null)
buckets[i] = new Bucket();

get:没有就返回-1

remove:没有就直接return

[思维问题]:

[英文数据结构或算法,为什么不用别的数据结构或算法]:

Bucket类都用final变量,表示容量恒定不变

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

找上一个的bucket[index] ,根据上一个来找下一个:

[一刷]:

  1. ListNode类中包括ListNode方法
  2. find函数也是返回前一个prev节点

[二刷]:

  1. find函数查找的是key,不是value

[三刷]:

  1. 后面三个函数都要调用find函数啊,不然find干嘛的
  2. 都要考虑prev.next不存在的情况

[四刷]:

  1. 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 实现哈希表的更多相关文章

  1. LeetCode 706. Design HashMap (设计哈希映射)

    题目标签:HashMap 题目让我们设计一个 hashmap, 有put, get, remove 功能. 建立一个 int array, index 是key, 值是 value,具体看code. ...

  2. 706. Design HashMap - LeetCode

    Question 706. Design HashMap Solution 题目大意:构造一个hashmap 思路:讨个巧,只要求key是int,哈希函数选择f(x)=x,规定key最大为100000 ...

  3. 【Leetcode_easy】706. Design HashMap

    problem 706. Design HashMap solution1: class MyHashMap { public: /** Initialize your data structure ...

  4. Leetcode PHP题解--D75 706. Design HashMap

    2019独角兽企业重金招聘Python工程师标准>>> D75 706. Design HashMap 题目链接 706. Design HashMap 题目分析 自行设计一个has ...

  5. HashMap分析 + 哈希表

    http://www.cnblogs.com/hzmark/archive/2012/12/24/HashMap.html http://www.cnblogs.com/xqzt/archive/20 ...

  6. Leetcode706.Design HashMap设计哈希映射

    不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get(key ...

  7. [leetcode] 706. Design HashMap

    题目 Design a HashMap without using any built-in hash table libraries. Implement the MyHashMap class: ...

  8. LeetCode 706 Design HashMap 解题报告

    题目要求 Design a HashMap without using any built-in hash table libraries. To be specific, your design s ...

  9. [LeetCode&Python] Problem 706. Design HashMap

    Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...

随机推荐

  1. 浅谈JS的数组遍历方法

    用过Underscore的朋友都知道,它对数组(集合)的遍历有着非常完善的API可以调用的,_.each()就是其中一个.下面就是一个简单的例子: var arr = [1, 2, 3, 4, 5]; ...

  2. [转]阿里巴巴十年Java架构师分享,会了这个知识点的人都去BAT了

    1.源码分析专题 详细介绍源码中所用到的经典设计思想,看看大牛是如何写代码的,提升技术审美.提高核心竞争力. 帮助大家寻找分析源码的切入点,在思想上来一次巨大的升华.知其然,并知其所以然.把知识变成自 ...

  3. Python3 练习2 列表和字典练习

    找出列表list中大于100的值,给字典dic的k1键,小于等于100的值,给字典dic的k2键 ''' 提示:创建字典的两种方式 ex: ''' v1 = [2,3,4,5,] v2 = 88 di ...

  4. linux命令行命令

    Linux命令行编辑快捷键: history 显示命令历史列表 ↑(Ctrl+p) 显示上一条命令 ↓(Ctrl+n) 显示下一条命令 !num 执行命令历史列表的第num条命令 !! 执行上一条命令 ...

  5. SqlServer查询某个表的列名称、说明、备注、类型等

    SELECT 表名 = case when a.colorder=1 then d.name else '' end, 表说明 = case when a.colorder=1 then isnull ...

  6. react使用ant-design组件库

    新建项目并引入组件 1,全局安装脚手架 npm install -g create-react-app 2,新建项目 create-react-app reactantd 3,安装组件 npm ins ...

  7. python之路——15

    王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 复习 1.迭代器 1.可迭代协议:含有iter 2.迭代器协议:含有iter和next 3.特点:节省内存, ...

  8. centos7+nginx+rtmp+ffmpeg搭建流媒体服务器(保存流目录与http目录不要随意配置,否则有权限问题)

    搭建nginx-http-flv-module升级代替rtmp模块,详情:https://github.com/winshining/nginx-http-flv-module/blob/master ...

  9. sed -i命令详解

    [root@www ~]# sed [-nefr] [动作] 选项与参数: -n :使用安静(silent)模式.在一般 sed 的用法中,所有来自 STDIN 的数据一般都会被列出到终端上.但如果加 ...

  10. Web项目中得到访问者的真实ip

    Web项目中得到访问者的真实ip 描述:最近要实现个功能是要记录管理员登录的真实ip,但在项目中如果直接使用request.getRemoteAddr()获得ip的话,获得的可能不是真实ip,是因为使 ...