Problem
Design a HashMap without using any built-in hash table libraries.
To be specific, your design should include these functions:
put(key, value) : Insert a (key, value) pair into the HashMap. If the value already exists in the HashMap, update the value.
get(key): Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
remove(key) : Remove the mapping for the value key if this map contains the mapping for the key.
Example:
MyHashMap hashMap = new MyHashMap();
hashMap.put(1, 1);
hashMap.put(2, 2);
hashMap.get(1); // returns 1
hashMap.get(3); // returns -1 (not found)
hashMap.put(2, 1); // update the existing value
hashMap.get(2); // returns 1
hashMap.remove(2); // remove the mapping for 2
hashMap.get(2); // returns -1 (not found)
Note:
All keys and values will be in the range of [0, 1000000].
The number of operations will be in the range of [1, 10000].
Please do not use the built-in HashMap library.
class MyHashMap { class ListNode { ListNode next; int key, val; ListNode(int key, int value) { this.key = key; this.val = value; } } class Bucket { final ListNode head = new ListNode(-1, -1); } final int size = 10000; final Bucket[] buckets = new Bucket[size]; /** Initialize your data structure here. */ public MyHashMap() { } //used to find the bucket private int hash(int key) { return key % size; } //used to find node by key in certain bucket private ListNode findNode(Bucket bucket, int key) { ListNode head = bucket.head; ListNode node = head; while (head != null && head.key != key) { //when head == null, or head.key == key, return its previous node node = head; head = head.next; } return node; } /** value will always be non-negative. */ public void put(int key, int value) { int i = hash(key); if (buckets[i] == null) buckets[i] = new Bucket(); ListNode pre = findNode(buckets[i], key); if (pre.next == null) { pre.next = new ListNode(key, value); } else { pre.next.val = 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) { int i = hash(key); if (buckets[i] == null) return -1; ListNode pre = findNode(buckets[i], key); if (pre.next == null) { return -1; } else { return pre.next.val; } } /** Removes the mapping of the specified value key if this map contains a mapping for the key */ public void remove(int key) { int i = hash(key); if (buckets[i] == null) return; ListNode pre = findNode(buckets[i], key); if (pre.next == null) return; else pre.next = pre.next.next; } } /** * 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); */
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/77182.html
摘要:題目鏈接題目分析自行設計一個。需要實現題目內指定的函數。思路我覺得這個沒什么好說的吧最終代碼若覺得本文章對你有用,歡迎用愛發電資助。 D75 706. Design HashMap 題目鏈接 706. Design HashMap 題目分析 自行設計一個hashmap。 需要實現題目內指定的函數。 思路 我覺得這個沒什么好說的吧… 最終代碼
摘要:題目鏈接和基本一樣,都可以用,但是大了之后會有很多無效的時間保存在里面,要的話,可能需要遍歷或者用輔助,時間復雜度超過,所以用一個來做,每次根據新的改變。 359. Logger Rate Limiter 題目鏈接:https://leetcode.com/problems... 和Design Hit Counter基本一樣,都可以用hashmap,但是timestamp大了之后會有...
Problem Design a search autocomplete system for a search engine. Users may input a sentence (at least one word and end with a special character #). For each character they type except #, you need to r...
摘要:首先建立按時間戳從大到小排列的,找到中的,出其中在中存在的,把每一個的推特鏈表放入,再從中取頭十條推特的放入結果數組。 Design Twitter Note 建立兩個HashMap,一個存user,一個存tweets。以及整型的時間戳timestamp。user的k-v pair是userId-follower_set,tweets的k-v pair是userId-tweets_li...
Problem Design an in-memory file system to simulate the following functions: ls: Given a path in string format. If it is a file path, return a list that only contains this files name. If it is a direc...
閱讀 3561·2023-04-26 02:10
閱讀 1298·2021-11-22 15:25
閱讀 1668·2021-09-22 10:02
閱讀 907·2021-09-06 15:02
閱讀 3469·2019-08-30 15:55
閱讀 600·2019-08-30 13:58
閱讀 2775·2019-08-30 12:53
閱讀 3042·2019-08-29 12:38