706. Design HashMap - LeetCode
Question

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的更多相关文章
- Leetcode PHP题解--D75 706. Design HashMap
2019独角兽企业重金招聘Python工程师标准>>> D75 706. Design HashMap 题目链接 706. Design HashMap 题目分析 自行设计一个has ...
- 【Leetcode_easy】706. Design HashMap
problem 706. Design HashMap solution1: class MyHashMap { public: /** Initialize your data structure ...
- [leetcode] 706. Design HashMap
题目 Design a HashMap without using any built-in hash table libraries. Implement the MyHashMap class: ...
- 【LeetCode】706. Design HashMap 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- LeetCode 706. Design HashMap (设计哈希映射)
题目标签:HashMap 题目让我们设计一个 hashmap, 有put, get, remove 功能. 建立一个 int array, index 是key, 值是 value,具体看code. ...
- 706. Design HashMap 实现哈希表
[抄题]: public MyHashMap() { 主函数里面是装非final变量的,如果没有,可以一个字都不写 } [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: ...
- LeetCode 706:设计哈希映射 Design HashMap
题目: 不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get ...
随机推荐
- Noob渗透笔记
靶机下载地址:https://www.vulnhub.com/entry/noob-1,746/ kali ip 信息收集 依旧我们先使用nmap扫描确定一下靶机ip nmap -sP 192.168 ...
- React 可视化开发工具 Shadow Widget 非正经入门(之五:指令式界面设计)
本系列博文从 Shadow Widget 作者的视角,解释该框架的设计要点.本篇解释 Shadow Widget 中类 Vue 的控制指令,与指令式界面设计相关. 1. 指令式界面设计 Vue 与 A ...
- 小程序完整对接 pingpp支付
小程序完整对接 pingpp支付 有几个先要条件: 小程序需要企业认证且开通支付功能,个人认证是无法使用支付功能的(小程序微信支付官网) pingpp 本身接入的企业服务器(即商户服务器)并不强制要求 ...
- 安卓性能测试之 adb shell 常用命令
pm list packages 列出包名adb shell pm list packages:列出所有的包名.adb shell dumpsys package:列出所有的安装应用的信息adb sh ...
- 判断H5页面是在小程序的webview环境中,还是在微信环境中,还是不在微信
<script src="https://res2.wx.qq.com/open/js/jweixin-1.6.0.js" type="text/javascrip ...
- Math类、Random类、System类、BigInteger类、BigDecimal类、Date类、SimpleDateFormat、Calendar类
Math类* A:Math类概述 * Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数. * B:成员方法 * public static int abs(int a) ...
- 在tomcat布置项目
1.将项目打成war包复制到tomcat-webapps 2.修改tomcat端口号 3.指定jdk 一.找到tomcat目录/bin 文件夹下的 catalina.bat文件 二.在文件中找到 ec ...
- 火狐浏览器Hackbar安装破解
1 下载 https://pan.baidu.com/s/18cKoJAam9by7AB168Im57g 64mt 下载后解压到一个固定文件夹下 2 安装 选择xpi进行安装 3 关闭插件更新 点击插 ...
- c语言实现双链表的基本操作—增删改查
//初始化 Node*InitList() { Node*head=(Node*)malloc(sizeof(Node)); if(NULL==head) { printf("内存分配失败! ...
- VUE(uni-app)+SSM 微信小程序
环境 jdk:1.8.0_181 tomcat:7.0.100 mysql:8.0.25 工具 ideaIU+Hbuilder 遇到的问题 1.需要跳转到注册在tobar中页面时,需使用 uni.sw ...