706. Design HashMap Easy
1/**
2 * [706] Design HashMap
3 *
4 * Design a HashMap without using any built-in hash table libraries.
5 * Implement the MyHashMap class:
6 *
7 * MyHashMap() initializes the object with an empty map.
8 * 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.
9 * 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.
10 * void remove(key) removes the key and its corresponding value if the map contains the mapping for the key.
11 *
12 *
13 * Example 1:
14 *
15 * Input
16 * ["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"]
17 * [[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
18 * Output
19 * [null, null, null, 1, -1, null, 1, null, -1]
20 * Explanation
21 * MyHashMap myHashMap = new MyHashMap();
22 * myHashMap.put(1, 1); // The map is now [[1,1]]
23 * myHashMap.put(2, 2); // The map is now [[1,1], [2,2]]
24 * myHashMap.get(1); // return 1, The map is now [[1,1], [2,2]]
25 * myHashMap.get(3); // return -1 (i.e., not found), The map is now [[1,1], [2,2]]
26 * myHashMap.put(2, 1); // The map is now [[1,1], [2,1]] (i.e., update the existing value)
27 * myHashMap.get(2); // return 1, The map is now [[1,1], [2,1]]
28 * myHashMap.remove(2); // remove the mapping for 2, The map is now [[1,1]]
29 * myHashMap.get(2); // return -1 (i.e., not found), The map is now [[1,1]]
30 *
31 *
32 * Constraints:
33 *
34 * 0 <= key, value <= 10^6
35 * At most 10^4 calls will be made to put, get, and remove.
36 *
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/design-hashmap/
41// discuss: https://leetcode.com/problems/design-hashmap/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45struct MyHashMap {
46 false
47 }
48
49
50/**
51 * `&self` means the method takes an immutable reference.
52 * If you need a mutable reference, change it to `&mut self` instead.
53 */
54impl MyHashMap {
55
56 fn new() -> Self {
57
58 }
59
60 fn put(&self, key: i32, value: i32) {
61
62 }
63
64 fn get(&self, key: i32) -> i32 {
65
66 }
67
68 fn remove(&self, key: i32) {
69
70 }
71}
72
73/**
74 * Your MyHashMap object will be instantiated and called as such:
75 * let obj = MyHashMap::new();
76 * obj.put(key, value);
77 * let ret_2: i32 = obj.get(key);
78 * obj.remove(key);
79 */
80
81// submission codes end
82
83#[cfg(test)]
84mod tests {
85 use super::*;
86
87 #[test]
88 fn test_706() {
89 }
90}
91
Back
© 2025 bowen.ge All Rights Reserved.