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. sudo rosdep init 出现 ERROR: cannot download default sources list from:错误解决方法

    关于安装ROS时出现的rosdep init错误 sudo rosdep init ERROR: cannot download default sources list from: https:// ...

  2. css3 弹性布局和多列布局

    弹性盒子基础 弹性盒子(Flexible Box)是css3中盒子模型的弹性布局,在传统的布局方式上增加了很多灵活性. 定义一个弹性盒子 在父盒子上定义display属性: #box{ display ...

  3. html 元素 强制不换行

    html 中 nowrap是用来强制不换行的 在排版中 对包裹plain text的标签使用nowrap属性即刻实现强制不换行. 如:<p nowrap>强制不换行</p>&l ...

  4. js 简易模块加载器 示例分析

    前端模块化 关注前端技术发展的各位亲们,肯定对模块化开发这个名词不陌生.随着前端工程越来越复杂,代码越来越多,模块化成了必不可免的趋势. 各种标准 由于javascript本身并没有制定相关标准(当然 ...

  5. ES6-11学习笔记--let

    新声明方式:let 1.不属于顶层对象 window 2.不允许重复声明 3.不存在变量提升 4.暂时性死区 5.块级作用域   原来var声明: var a = 5; console.log(a); ...

  6. Git使用方法以及出现的bug解决方案

    git常用命令 1.本地库初始化: git init 2.设置签名 (1)项目级别(项目里面) git config user.name xxx git config user.email xxx ( ...

  7. vue里面v-for显示红色波浪线

    vue里面使用v-for代码显示红色的波浪线,解决办法: before: <div v-for="tmsgs in msg.message"></div> ...

  8. 设计模式学习笔记(十四)责任链模式实现以及在Filter中的应用

    责任链模式(Chain Of Responsibility Design Pattern),也叫做职责链,是将请求的发送和接收解耦,让多个接收对象都有机会处理这个请求.当有请求发生时,可将请求沿着这条 ...

  9. Mybatis个人笔记

    Mybatis 简介 官网地址:mybatis – MyBatis 3 | 简介 MyBatis 是一款优秀的持久层框架,它支持自定义 SQL.存储过程以及高级映射.MyBatis 免除了几乎所有的 ...

  10. 用来创建用户docker registry认证的Secret

    集群环境:1.k8s用的是二进制方式安装2.操作系统是linux (centos)3.操作系统版本为 7.4/7.94.k8s的应用管理.node管理.pod管理等用rancher.k8s令牌以及ma ...