• 706. Design HashMap


    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;
        }
    }
    
  • 相关阅读:
    MVC CONTROLS TOOLKIT
    activemq Example
    OWASP
    ActiveMQ持久化消息的三种方式
    sqlyog
    dotnet压缩
    asp.net ajax 环境 c#与js互调
    asp.net 初步入门使用正则抓取网页信息
    用ASP.NET with C# 绘制曲线图(Curve图)转
    asp.net 中使用excel组件权限设置
  • 原文地址:https://www.cnblogs.com/okokabcd/p/9374563.html
Copyright © 2020-2023  润新知