题目链接

O(1)时间插入、删除和获取随机元素

题目描述

image.png

实现代码

个人实现代码:

  1. class RandomizedSet {
  2. Map<Integer, Boolean> containMap = new HashMap();
  3. int size = 0;
  4. Random rand = new Random();
  5. public RandomizedSet() {
  6. }
  7. public boolean insert(int val) {
  8. if(getVal(val)) {
  9. return false;
  10. }
  11. containMap.put(val, true);
  12. size++;
  13. return true;
  14. }
  15. public boolean remove(int val) {
  16. if(getVal(val)) {
  17. containMap.remove(val);
  18. size--;
  19. return true;
  20. }
  21. return false;
  22. }
  23. public int getRandom() {
  24. int index = rand.nextInt(size) + 1;
  25. int val = 0;
  26. for(Map.Entry<Integer, Boolean> entry : containMap.entrySet()) {
  27. val = entry.getKey();
  28. if(--index == 0) {
  29. break;
  30. }
  31. }
  32. return val;
  33. }
  34. private Boolean getVal(int key) {
  35. return containMap.get(key) != null;
  36. }
  37. }
  38. /**
  39. * Your RandomizedSet object will be instantiated and called as such:
  40. * RandomizedSet obj = new RandomizedSet();
  41. * boolean param_1 = obj.insert(val);
  42. * boolean param_2 = obj.remove(val);
  43. * int param_3 = obj.getRandom();
  44. */

参考大佬实现思想:

  1. 定义一个map,一个数组nums,和一个整型变量size
  2. map的key为元素的值,value为该元素在nums数组中的存放索引;
  3. 当insert时,通过map判断是否存在,不存在则map、nums中都插入,同时size+1;
  4. 当remove时,通过map判断是否存在,存在则通过map的value找到在数组nums中的位置,将其与size位置即最后一个位置的元素进行交换,size-1;

实现代码:

  1. class RandomizedSet {
  2. static int[] nums = new int[200010];
  3. Random random = new Random();
  4. Map<Integer, Integer> map = new HashMap<>();
  5. int idx = -1;
  6. public boolean insert(int val) {
  7. if (map.containsKey(val)) return false;
  8. nums[++idx] = val;
  9. map.put(val, idx);
  10. return true;
  11. }
  12. public boolean remove(int val) {
  13. if (!map.containsKey(val)) return false;
  14. int loc = map.remove(val);
  15. if (loc != idx) map.put(nums[idx], loc);
  16. nums[loc] = nums[idx--];
  17. return true;
  18. }
  19. public int getRandom() {
  20. return nums[random.nextInt(idx + 1)];
  21. }
  22. }

PS:个人参考思想以后写的代码没AC,离谱:

  1. class RandomizedSet {
  2. Map<Integer, Integer> map = new HashMap();
  3. int[] nums = new int[100002];
  4. int size = 0;
  5. Random rand = new Random();
  6. public RandomizedSet() {
  7. }
  8. public boolean insert(int val) {
  9. if(map.containsKey(val)) {
  10. return false;
  11. }
  12. map.put(val, size);
  13. nums[size++] = val;
  14. return true;
  15. }
  16. public boolean remove(int val) {
  17. Integer location = map.get(val);
  18. if(location == null) {
  19. return false;
  20. }
  21. map.remove(val);
  22. nums[location] = nums[--size];
  23. map.put(nums[location], location);
  24. return true;
  25. }
  26. public int getRandom() {
  27. return nums[rand.nextInt(size)];
  28. }
  29. }
  30. /**
  31. * Your RandomizedSet object will be instantiated and called as such:
  32. * RandomizedSet obj = new RandomizedSet();
  33. * boolean param_1 = obj.insert(val);
  34. * boolean param_2 = obj.remove(val);
  35. * int param_3 = obj.getRandom();
  36. */