Question

706. Design HashMap

Solution

题目大意:构造一个hashmap

思路:讨个巧,只要求key是int,哈希函数选择f(x)=x,规定key最大为1000000,那构造一个1000000的数组

Java实现:

class MyHashMap {

    int[] table;

    /** Initialize your data structure here. */
public MyHashMap() {
table = new int[1000000];
Arrays.fill(table, -1);
} /** value will always be non-negative. */
public void put(int key, int value) {
table[key] = 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) {
return table[key];
} /** Removes the mapping of the specified value key if this map contains a mapping for the key */
public void remove(int key) {
table[key] = -1;
}
} /**
* 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);
*/

参考别人的

class MyHashMap {
final Bucket[] buckets = new Bucket[10000]; public void put(int key, int value) {
int i = idx(key);
if (buckets[i] == null)
buckets[i] = new Bucket();
ListNode prev = find(buckets[i], key);
if (prev.next == null)
prev.next = new ListNode(key, value);
else prev.next.val = value;
} public int get(int key) {
int i = idx(key);
if (buckets[i] == null)
return -1;
ListNode node = find(buckets[i], key);
return node.next == null ? -1 : node.next.val;
} public void remove(int key) {
int i = idx(key);
if (buckets[i] == null) return;
ListNode prev = find(buckets[i], key);
if (prev.next == null) return;
prev.next = prev.next.next;
} int idx(int key) { return Integer.hashCode(key) % buckets.length;} ListNode find(Bucket bucket, int key) {
ListNode node = bucket.head, prev = null;
while (node != null && node.key != key) {
prev = node;
node = node.next;
}
return prev;
}
} class Bucket {
final ListNode head = new ListNode(-1, -1);
} class ListNode {
int key, val;
ListNode next; ListNode(int key, int val) {
this.key = key;
this.val = val;
}
}

706. Design HashMap - LeetCode的更多相关文章

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

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

  2. 【Leetcode_easy】706. Design HashMap

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

  3. [leetcode] 706. Design HashMap

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

  4. 【LeetCode】706. Design HashMap 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. LeetCode 706 Design HashMap 解题报告

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

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

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

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

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

  8. 706. Design HashMap 实现哈希表

    [抄题]: public MyHashMap() {  主函数里面是装非final变量的,如果没有,可以一个字都不写 } [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: ...

  9. LeetCode 706:设计哈希映射 Design HashMap

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

随机推荐

  1. Linux下切换python2和python3

    为什么需要有两个版本的Python Python2和Python3不兼容是每个接触过Python的开发者都知道的事,虽说Python3是未来,但是仍然有很多项目采用Python2开发.Linux的许多 ...

  2. 5_系统的可控性_Controllability

  3. 一个用于学习的react项目

    React-element 根据开源项目 vue-sell进行的开发,将其改造成了react的项目.在开始学习vue的时候就是用的这个项目,发现效果不错,所以在学习React也使用了此项目. 目的:将 ...

  4. 2015 年十佳 HTML5 应用

    前言 优秀的前端工程师戴着脚铐跳舞,究竟能把 HTML5 的体验推进到什么程度? 这些 Web apps 是我们运营云集浏览器的网上应用店一年来,我选出的十佳 Web apps.其中参考了同事们的意见 ...

  5. [译] Facebook:我们是如何构建第一个跨平台的 React Native APP

    英文原文(需FQ):https://code.facebook.com/posts/1189117404435352/ 早些时候,我们介绍过iOS版的React Native. React Nativ ...

  6. div 底部固定方法(不用position定位)

    方法一:全局增加一个负值下边距等于底部高度 <style> html, body { height: 100%; margin: 0; } .content { padding: 20px ...

  7. 前端调试利器 - Charles

    Docs 开发之 Charles 配置指南 1.下载与安装 charles-proxy-4.1.4 .dmg56.12 MB已存到云盘下载 2.激活 使用公司正版license 激活 安装证书 点击证 ...

  8. python-for循环跳过第一行

    代码: for i in data[1:]: 即可跳过第一行

  9. 【Weex笔记】-- Animate.css的使用

    animate.css是一个使用CSS3的animation制作的动画效果的CSS集合,里面预设了很多种常用的动画,且使用非常简单.本文将详细介绍animate.css的使用. 一,安装辅助依赖 np ...

  10. 基于Apache Hudi和Debezium构建CDC入湖管道

    从 Hudi v0.10.0 开始,我们很高兴地宣布推出适用于 Deltastreamer 的 Debezium 源,它提供从 Postgres 和 MySQL 数据库到数据湖的变更捕获数据 (CDC ...