|
| 1 | +/** |
| 2 | + * [0706] 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 | + */ |
| 38 | +pub 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 | + |
| 45 | +const TABLE_SIZE: usize = 32; |
| 46 | + |
| 47 | +#[derive(Default)] |
| 48 | +struct MyHashMap { |
| 49 | + table: [Vec<(i32, i32)>; TABLE_SIZE], |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * `&self` means the method takes an immutable reference. |
| 54 | + * If you need a mutable reference, change it to `&mut self` instead. |
| 55 | + */ |
| 56 | +impl MyHashMap { |
| 57 | + fn new() -> Self { |
| 58 | + Default::default() |
| 59 | + } |
| 60 | + |
| 61 | + fn put(&mut self, key: i32, value: i32) { |
| 62 | + let i = (key as usize) % TABLE_SIZE; |
| 63 | + if let Some(j) = self.table[i].iter().position(|&(k, _)| k == key) { |
| 64 | + self.table[i][j].1 = value; |
| 65 | + } else { |
| 66 | + self.table[i].push((key, value)); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + fn get(&mut self, key: i32) -> i32 { |
| 71 | + let i = (key as usize) % TABLE_SIZE; |
| 72 | + self.table[i] |
| 73 | + .iter() |
| 74 | + .position(|&(k, _)| k == key) |
| 75 | + .map(|j| self.table[i][j].1) |
| 76 | + .unwrap_or(-1) |
| 77 | + } |
| 78 | + |
| 79 | + fn remove(&mut self, key: i32) { |
| 80 | + let i = (key as usize) % TABLE_SIZE; |
| 81 | + if let Some(j) = self.table[i].iter().position(|&(k, _)| k == key) { |
| 82 | + self.table[i].remove(j); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Your MyHashMap object will be instantiated and called as such: |
| 89 | + * let obj = MyHashMap::new(); |
| 90 | + * obj.put(key, value); |
| 91 | + * let ret_2: i32 = obj.get(key); |
| 92 | + * obj.remove(key); |
| 93 | + */ |
| 94 | + |
| 95 | +// submission codes end |
| 96 | + |
| 97 | +#[cfg(test)] |
| 98 | +mod tests { |
| 99 | + use super::*; |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn test_0706_example_1() { |
| 103 | + let mut my_hash_map = MyHashMap::new(); |
| 104 | + my_hash_map.put(1, 1); // The map is now [[1,1]] |
| 105 | + my_hash_map.put(2, 2); // The map is now [[1,1], [2,2]] |
| 106 | + assert_eq!(my_hash_map.get(1), 1); // return 1, The map is now [[1,1], [2,2]] |
| 107 | + assert_eq!(my_hash_map.get(3), -1); // return -1 (i.e., not found), The map is now [[1,1], [2,2]] |
| 108 | + my_hash_map.put(2, 1); // The map is now [[1,1], [2,1]] (i.e., update the existing value) |
| 109 | + assert_eq!(my_hash_map.get(2), 1); // return 1, The map is now [[1,1], [2,1]] |
| 110 | + my_hash_map.remove(2); // remove the mapping for 2, The map is now [[1,1]] |
| 111 | + assert_eq!(my_hash_map.get(2), -1); // return -1 (i.e., not found), The map is now [[1,1]] |
| 112 | + } |
| 113 | +} |
0 commit comments