LeetCode(Linked List)705. Design HashSet

╰半夏微凉° 2023-09-24 20:10 130阅读 0赞

1.问题

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

Implement MyHashSet class:

  • void add(key) Inserts the value key into the HashSet.
  • bool contains(key) Returns whether the value key exists in the HashSet or not.
  • void remove(key) Removes the value key in the HashSet. If key does not exist in the HashSet, do nothing.
    Example 1:

Input
[“MyHashSet”, “add”, “add”, “contains”, “contains”, “add”, “contains”, “remove”, “contains”]
[[], [1], [2], [1], [3], [2], [2], [2], [2]]
Output
[null, null, null, true, false, null, true, null, false]

Explanation
MyHashSet myHashSet = new MyHashSet();
myHashSet.add(1); // set = [1]
myHashSet.add(2); // set = [1, 2]
myHashSet.contains(1); // return True
myHashSet.contains(3); // return False, (not found)
myHashSet.add(2); // set = [1, 2]
myHashSet.contains(2); // return True
myHashSet.remove(2); // set = [1]
myHashSet.contains(2); // return False, (already removed)

Constraints:

  • 0 <= key <= 106
  • At most 104 calls will be made to add, remove, and contains.

2. 解题思路

方法1:

1.定义arr为boolean的数组
2.arr的数组长度为1000000+1 (0 <= key <= 106)
3.添加,如果数组arr[key] = true
4.删除,如果数组arr[key] = false
5.如果此集合包含指定元素,则返回 true

方法2:

1.定义Node,head为null

  1. 如果head为null,head的值为key;判断添加的值是否在node里,如果不是添加
    3.如果head为null,return;如果head.val==key,删除,head=head.next;否者遍历直到node的值和key值相等
    4.如果head为null返回false,如果head的值等于key返回true;否则遍历直到node的值和key值相等返回true

3. 代码

代码1:

  1. class MyHashSet {
  2. boolean[] arr;//1.定义arr为boolean的数组
  3. public MyHashSet() {
  4. arr = new boolean[1000000+1];//2.arr的数组长度为1000000+1 (0 <= key <= 106)
  5. }
  6. public void add(int key) {
  7. //3.添加,如果数组arr[key] = true
  8. arr[key] = true;
  9. }
  10. public void remove(int key) {
  11. //4.删除,如果数组arr[key] = false
  12. arr[key] = false;
  13. }
  14. public boolean contains(int key) {
  15. //5.如果此集合包含指定元素,则返回 true
  16. return arr[key];
  17. }
  18. }

代码2:

  1. class Node{
  2. Node next;
  3. int val;
  4. public Node(int key){
  5. this.val=key;
  6. this.next=null;
  7. }
  8. }
  9. class MyHashSet {
  10. Node head;
  11. public MyHashSet() {
  12. //1.定义Node,head为null
  13. head=null;
  14. }
  15. public void add(int key) {
  16. //2. 如果head为null,head的值为key;判断添加的值是否在node里,如果不是添加
  17. if(head==null)
  18. {
  19. head= new Node(key);
  20. return;
  21. }
  22. boolean doesExists=contains(key);
  23. if(!doesExists){
  24. Node temp=head;
  25. while(temp.next!=null)
  26. temp=temp.next;
  27. temp.next=new Node(key);
  28. }
  29. }
  30. public void remove(int key) {
  31. //3.如果head为null,return;如果head.val==key,删除,head=head.next;否者遍历直到node的值和key值相等
  32. if(head==null) return;
  33. if(head.val==key){
  34. head=head.next;
  35. return;
  36. }
  37. Node temp=head;
  38. while(temp.next!=null){
  39. if(temp.next.val==key) temp.next=temp.next.next;
  40. temp=temp.next;
  41. }
  42. }
  43. public boolean contains(int key) {
  44. //4.如果head为null返回false,如果head的值等于key返回true;否则遍历直到node的值和key值相等返回true
  45. if(head==null) return false;
  46. if(head.val==key) return true;
  47. Node temp=head;
  48. while(temp.next!=null){
  49. if(temp.next.val==key) return true;
  50. temp=temp.next;
  51. }
  52. return false;
  53. }
  54. }
  55. /**
  56. * Your MyHashSet object will be instantiated and called as such:
  57. * MyHashSet obj = new MyHashSet();
  58. * obj.add(key);
  59. * obj.remove(key);
  60. * boolean param_3 = obj.contains(key);
  61. */

发表评论

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

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

相关阅读