Skip to content

Latest commit

 

History

History
205 lines (168 loc) · 5.18 KB

706.design_hashmap.md

File metadata and controls

205 lines (168 loc) · 5.18 KB
  1. Design HashMap

简单

https://leetcode-cn.com/problems/design-hashmap/

Design a HashMap without using any built-in hash table libraries.

Implement the MyHashMap class:

MyHashMap() initializes the object with an empty map.
void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value.
int get(int key) returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
void remove(key) removes the key and its corresponding value if the map contains the mapping for the key.
Example 1:

Input
["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"]
[[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
Output
[null, null, null, 1, -1, null, 1, null, -1]


Explanation
MyHashMap myHashMap = new MyHashMap();
myHashMap.put(1, 1); // The map is now [[1,1]]
myHashMap.put(2, 2); // The map is now [[1,1], [2,2]]
myHashMap.get(1);    // return 1, The map is now [[1,1], [2,2]]
myHashMap.get(3);    // return -1 (i.e., not found), The map is now [[1,1], [2,2]]
myHashMap.put(2, 1); // The map is now [[1,1], [2,1]] (i.e., update the existing value)
myHashMap.get(2);    // return 1, The map is now [[1,1], [2,1]]
myHashMap.remove(2); // remove the mapping for 2, The map is now [[1,1]]
myHashMap.get(2);    // return -1 (i.e., not found), The map is now [[1,1]]

Constraints:

0 <= key, value <= 106
At most 104 calls will be made to put, get, and remove.

相关企业

  • 亚马逊 Amazon|10
  • 微软 Microsoft|7
  • 领英 LinkedIn|5
  • 甲骨文 Oracle|4
  • 苹果 Apple|3
  • 高盛集团 Goldman Sachs|3
  • Facebook|2

相关标签

  • Design
  • Array
  • Hash Table
  • Linked List
  • Hash Function

相似题目

  • Design HashSet 简单

open hashing/ closed addressing / seperate chaining

Use LinkedList[] array

class MyHashMap {

    private class Pair {
        private int key;
        private int value;

        public Pair(int key, int value){
            this.key = key;
            this.value = value;
        }

        public int getKey(){
            return this.key;
        }
        public int getValue(){
            return this.value;
        }
        public void setKey(int key){
            this.key = key;
        }
        public void setValue(int value){
            this.value = value;
        }
    }

    private static final int BASE_LENGTH = 997;
    private LinkedList[] data;

    public MyHashMap() {
        this.data = new LinkedList[this.BASE_LENGTH];
        for (int i = 0; i < BASE_LENGTH; i++){
            this.data[i] = new LinkedList<Pair>();
        }
    }

    private int hash(int key){
        return key % this.BASE_LENGTH;
    }
    
    public void put(int key, int value) {
        int hashposition = this.hash(key);
        Iterator<Pair> dataIterator = this.data[hashposition].iterator();
        while (dataIterator.hasNext()){
            Pair currpair = dataIterator.next();
            if (key == currpair.getKey()){ // if exist then change
                currpair.setValue(value);
                return;
            }
        }
        this.data[hashposition].addLast(new Pair(key, value)); // if not exist then add 
    }
    
    public int get(int key) {
        int hashposition = this.hash(key);
        Iterator<Pair> dataIterator = this.data[hashposition].iterator();
        while (dataIterator.hasNext()){
            Pair currpair = dataIterator.next();
            if (key == currpair.getKey()){ // if exist then return value
                return currpair.getValue();
            }
        }
        return -1;
    }
    
    public void remove(int key) {
        int hashposition = this.hash(key);
        Iterator<Pair> dataIterator = this.data[hashposition].iterator();
        while (dataIterator.hasNext()){
            Pair currpair = dataIterator.next();
            if (key == currpair.getKey()){ // if exist then return value
                this.data[hashposition].remove(currpair);
                return;
            }
        }
    }
}

/**
 * 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);
 */

use java HashMap class/ py dict()

java

class MyHashMap {
    HashMap<Integer, Integer> data;
    public MyHashMap() {
        this.data = new HashMap<Integer, Integer>();
    }
    
    public void put(int key, int value) {
        this.data.put(key, value);
    }
    
    public int get(int key) {
        return this.data.getOrDefault(key, -1);
    }
    
    public void remove(int key) {
        this.data.remove(key);
    }
}

python

class MyHashMap:

    def __init__(self):
        self.data = dict()

    def put(self, key: int, value: int) -> None:
        self.data[key] = value

    def get(self, key: int) -> int:
        if key in self.data.keys():
            return self.data[key]
        return -1

    def remove(self, key: int) -> None:
        if key in self.data.keys():
            self.data.pop(key) # == del self.data[key]
 

# Your MyHashMap object will be instantiated and called as such:
# obj = MyHashMap()
# obj.put(key,value)
# param_2 = obj.get(key)
# obj.remove(key)