博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
706. Design HashMap - LeetCode
阅读量:7020 次
发布时间:2019-06-28

本文共 2275 字,大约阅读时间需要 7 分钟。

  hot3.png

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;    }}

转载于:https://my.oschina.net/yysue/blog/1864017

你可能感兴趣的文章
逾期率的水有多深,你知道吗?
查看>>
服务网关zuul之二:过滤器--请求过滤执行过程(源码分析)
查看>>
goto语句的升级版,setjmp,longjmp
查看>>
CentOS7使用firewalld打开关闭防火墙与端口[转]
查看>>
Eclipse-Java代码规范和质量检查插件-Checkstyle
查看>>
c语言中各种数据类型的长度
查看>>
TEST mathjax
查看>>
修改web项目的启动页
查看>>
居中显示
查看>>
Java的不定参数(eg:Object...)(转)
查看>>
[编程] C语言循环结构计算π的值
查看>>
C/C++下scanf的%匹配以及过滤字符串问题
查看>>
全内存的redis用习惯了?那能突破内存限制类redis产品ssdb呢?
查看>>
17秋 软件工程 第六次作业 Beta冲刺
查看>>
html5--6-23 CSS3中的文字与字体
查看>>
使用腾讯云无服务器云函数(SCF)分析天气数据
查看>>
Android系统编译错误Note: Some input files use or override a deprecated API. 解决办法【转】...
查看>>
Redis进阶实践之四Redis的基本数据类型
查看>>
006-Spring Boot自动配置-Condition、Conditional、Spring提供的Conditional自动配置
查看>>
URAL Palindromic Contest
查看>>