LeetCode 706. Design HashMap

原题链接在这里:https://leetcode.com/problems/design-hashmap/

题目:

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.

题解:

Like HashMap, there is an array of link list node.

Get the hash and find the position in array and go through each node in that list to check the key.

Time Complexity: put, O(1). find, O(1). remove, O(1).

Space: O(size). size of array.

AC Java:

 1 class MyHashMap {
 2     int size = 100000;
 3     Node [] arr;
 4
 5     /** Initialize your data structure here. */
 6     public MyHashMap() {
 7         arr = new Node[size];
 8         for(int i = 0; i < arr.length; i++){
 9             arr[i] = new Node(-1, -1);
10         }
11     }
12
13     /** value will always be non-negative. */
14     public void put(int key, int value) {
15         Node p = findNode(key);
16
17         if(p.next == null){
18             p.next = new Node(key, value);
19         }else{
20             p.next.val = value;
21         }
22     }
23
24     private Node findNode(int key){
25         int hash = getHash(key);
26         Node p = arr[hash];
27
28         while(p.next != null && p.next.key != key){
29             p = p.next;
30         }
31
32         return p;
33     }
34
35     private int getHash(int n){
36         return n % size;
37     }
38
39     /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
40     public int get(int key) {
41         Node p = findNode(key);
42
43         if(p.next == null){
44             return -1;
45         }
46
47         return p.next.val;
48     }
49
50     /** Removes the mapping of the specified value key if this map contains a mapping for the key */
51     public void remove(int key) {
52         Node p = findNode(key);
53
54         if(p.next == null){
55             return;
56         }
57
58         p.next = p.next.next;
59     }
60 }
61
62 class Node{
63     int key;
64     int val;
65     Node next;
66     public Node(int key, int val){
67         this.key = key;
68         this.val = val;
69     }
70 }
71
72 /**
73  * Your MyHashMap object will be instantiated and called as such:
74  * MyHashMap obj = new MyHashMap();
75  * obj.put(key,value);
76  * int param_2 = obj.get(key);
77  * obj.remove(key);
78  */

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12196080.html

时间: 2024-07-29 15:00:22

LeetCode 706. Design HashMap的相关文章

LeetCode 706. Design HashMap (设计哈希映射)

题目标签:HashMap 题目让我们设计一个 hashmap, 有put, get, remove 功能. 建立一个 int array, index 是key, 值是 value,具体看code. Java Solution: Runtime: 76 ms, faster than 27.53% Memory Usage: 58.2 MB, less than 31.57% 完成日期:03/18/2019 关键点:int array class MyHashMap { int [] map;

706. Design HashMap - LeetCode

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]; Ar

706. Design HashMap

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.

[LeetCode] Design HashMap 设计HashMap

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.

LeetCode 642. Design Search Autocomplete System

原题链接在这里:https://leetcode.com/problems/design-search-autocomplete-system/ 题目: 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 ex

LeetCode 604. Design Compressed String Iterator (设计压缩字符迭代器)

Design and implement a data structure for a compressed string iterator. It should support the following operations: next and hasNext. The given compressed string will be in the form of each letter followed by a positive integer representing the numbe

【Leetcode】Design Twitter

题目链接: 题目: Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user's news feed. Your design should support the following methods: postTweet(userId, tweet

leetcode 355 Design Twitte

题目连接 https://leetcode.com/problems/design-twitter Design Twitte Description Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user’s news feed. Your de

[LeetCode] 534. Design TinyURL 设计短网址

Note: For the coding companion problem, please see: Encode and Decode TinyURL. How would you design a URL shortening service that is similar to TinyURL? Background:TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com