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 ...
随机推荐
- PythonStudy——Python 内存池机制 (Memory pool mechanism) Pymalloc
Python是如何进行内存管理-内存池机制 Pymalloc Python引用了一个内存池(memory pool)机制,即Pymalloc机制(malloc:n.分配内存),用于对小块内存的申请和释 ...
- zombodb sql functions 说明
zombodb 提供了好多方便的sql 函数工具类以及帮助函数 查看zombodb 版本zdb.version() select * from zdb.version(); version ----- ...
- lua tasklib 之lumen 分析
sched.sleep分析 sleep会填充M.running_task.waitds数据表示当前task需要等待,最后yield出去到主线程 M.sleep = function (timeout) ...
- centos7怎么查看、打开和关闭防火墙
使用centos7会发现,用centos6以前的方式查看.打开和关闭防火墙都无效了.这是因为centos7的防火墙改用firewalld,而不再使用iptables了 查看centos7的防火墙的状态 ...
- python pytz时区设置模块
如果你的程序要考虑时区,可以使用pytz.pytz官方文档:http://pytz.sourceforge.net/我使用的python版本:3.7.1 datetime模块中有tzinfo相关的东西 ...
- django 神奇的双下划线,通过外键的三种查询方式
一,用于跨表操作 只要是object后面字符串都是用双下划线__.其它地方用点. 如:的values中的group_code__name.group_code是一个外键 def list(reques ...
- VUE中如何优雅的动态绑定长按事件
答案没有: 图片是从后端传过来, 加到imgTarget属性,实现长按点击删除该图片 let img = document.createElement('img'); img.src = " ...
- <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 ...
- 20165312 2017-2018-2 《JAVA程序设计》第5周学习总结
20165312 2017-2018-2 <JAVA程序设计>第5周学习总结 一.本周学习内容总结 总的来说,本周学习较吃力,在理解第十章的代码时速度较慢. 内部类 内部类是定义在一个类中 ...
- 杂谈2.py
tuple用圆括号括住的项的列表,这些项不能改变,括号内的值都是相关的 dictionary 已经配对的键和值的列表,用花括号括住 当创建对象并在其中存储信息的时候变量的类型就确定啦 type(obj ...