LeetCode(Linked List)706. Design HashMap

怼烎@ 2023-09-23 22:48 101阅读 0赞

1.问题

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.

2. 解题思路

方法1:

1.定义arr数组
2.arr的数组长度为1000000+1 (0 <= key <= 106),将数组值填充为-1;
3.arr的key为value
4.取出key的value值
5.将key的value值设定为-1

方法2:

使用链表的方式

3. 代码

代码1:

  1. class MyHashMap {
  2. int[] arr;//1.定义arr数组
  3. public MyHashMap() {
  4. arr=new int[1000000+1];//2.arr的数组长度为1000000+1 (0 <= key <= 106),将数组值填充为-1;
  5. Arrays.fill(arr,-1);
  6. }
  7. public void put(int key, int value) {
  8. //3.arr的key为value
  9. arr[key]=value;
  10. }
  11. public int get(int key) {
  12. //4.取出key的value值
  13. return arr[key];
  14. }
  15. public void remove(int key) {
  16. //5.将key的value值设定为-1
  17. arr[key]=-1;
  18. }
  19. }
  20. /**
  21. * Your MyHashMap object will be instantiated and called as such:
  22. * MyHashMap obj = new MyHashMap();
  23. * obj.put(key,value);
  24. * int param_2 = obj.get(key);
  25. * obj.remove(key);
  26. */

代码2:

  1. class MyHashMap{
  2. final ListNode[] nodes = new ListNode[10000];//1.10000 calls will be made to put, get, and remove.因此新建ListNode的数组
  3. public void put(int key, int value){
  4. int i = idx(key);
  5. if(nodes[i] == null)
  6. nodes[i] = new ListNode(-1, -1);
  7. ListNode prev = find(nodes[i], key);
  8. if(prev.next == null)
  9. prev.next = new ListNode(key, value);
  10. else prev.next.val = value;
  11. }
  12. public int get(int key){
  13. int i = idx(key);
  14. if(nodes[i] == null)
  15. return -1;
  16. ListNode node = find(nodes[i], key);
  17. return node.next == null ? -1 : node.next.val;
  18. }
  19. public void remove(int key){
  20. int i = idx(key);
  21. if(nodes[i] != null){
  22. ListNode prev = find(nodes[i], key);
  23. if(prev.next != null)
  24. prev.next = prev.next.next;
  25. }
  26. }
  27. int idx(int key){
  28. return Integer.hashCode(key) % nodes.length;}
  29. ListNode find(ListNode bucket, int key){
  30. ListNode node = bucket, prev = null;
  31. for(; node != null && node.key != key; node = node.next)
  32. prev = node;
  33. return prev;
  34. }
  35. class ListNode{
  36. int key, val;
  37. ListNode next;
  38. ListNode(int key, int val){
  39. this.key = key;
  40. this.val = val;
  41. }
  42. }
  43. }

以上的解题思路基本相同

  1. class MyHashMap
  2. {
  3. ListNode[] nodes = new ListNode[10000];//1.10000 calls will be made to put, get, and remove.因此新建ListNode的数组
  4. public int get(int key)
  5. {
  6. int index = getIndex(key);//获取当前值的索引位置
  7. ListNode prev = findElement(index, key);//
  8. return prev.next == null ? -1 : prev.next.val;
  9. }
  10. public void put(int key, int value)//从key中得到索引值,并查询结点,如果没有则添加新的结点
  11. {
  12. int index = getIndex(key);
  13. ListNode prev = findElement(index, key);
  14. if (prev.next == null)
  15. prev.next = new ListNode(key, value);
  16. else
  17. prev.next.val = value;
  18. }
  19. public void remove(int key)//从key中得到索引值,并查询结点,如果有则删除结点
  20. {
  21. int index = getIndex(key);
  22. ListNode prev = findElement(index, key);
  23. if(prev.next != null)
  24. prev.next = prev.next.next;
  25. }
  26. private int getIndex(int key)//获取当前值的索引位置
  27. {
  28. return Integer.hashCode(key) % nodes.length;
  29. }
  30. private ListNode findElement(int index, int key)//查找元素
  31. {
  32. if(nodes[index] == null)//如果索引的值为null,链表的索引和值为-1
  33. return nodes[index] = new ListNode(-1, -1);
  34. ListNode prev = nodes[index];
  35. while(prev.next != null && prev.next.key != key)//当prev.next != null && prev.next.key != key,查找元素
  36. {
  37. prev = prev.next;
  38. }
  39. return prev;
  40. }
  41. private static class ListNode
  42. {
  43. int key, val;
  44. ListNode next;
  45. ListNode(int key, int val)
  46. {
  47. this.key = key;
  48. this.val = val;
  49. }
  50. }
  51. }

发表评论

表情:
评论列表 (有 0 条评论,101人围观)

还没有评论,来说两句吧...

相关阅读