首页 > 代码库 > LeetCode Insert Delete GetRandom O(1)

LeetCode Insert Delete GetRandom O(1)

原题链接在这里:https://leetcode.com/problems/insert-delete-getrandom-o1/?tab=Description

题目:

Design a data structure that supports all following operations in average O(1) time.

 

  1. insert(val): Inserts an item val to the set if not already present.
  2. remove(val): Removes an item val from the set if present.
  3. getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned.

 

Example:

// Init an empty set.
RandomizedSet randomSet = new RandomizedSet();

// Inserts 1 to the set. Returns true as 1 was inserted successfully.
randomSet.insert(1);

// Returns false as 2 does not exist in the set.
randomSet.remove(2);

// Inserts 2 to the set, returns true. Set now contains [1,2].
randomSet.insert(2);

// getRandom should return either 1 or 2 randomly.
randomSet.getRandom();

// Removes 1 from the set, returns true. Set now contains [2].
randomSet.remove(1);

// 2 was already in the set, so return false.
randomSet.insert(2);

// Since 2 is the only number in the set, getRandom always return 2.
randomSet.getRandom();

题解:

用list保存insert进来的数字. 利用HashMap<Integer, Integer> hm保存insert进来数字和对应的index.

insert, remove时对应更改list, hm.

getRandom在list上用random index get一个数.

Time Complexity: inset, O(1). remove, O(1). getRandom, O(1).

Space: O(n).

AC Java:

 1 public class RandomizedSet {
 2     List<Integer> ls;
 3     HashMap<Integer, Integer> hm;
 4     Random rand;
 5     
 6     /** Initialize your data structure here. */
 7     public RandomizedSet() {
 8         ls = new ArrayList<Integer>();
 9         hm = new HashMap<Integer, Integer>();
10         rand = new Random();
11     }
12     
13     /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
14     public boolean insert(int val) {
15         if(hm.containsKey(val)){
16             return false;
17         }
18         ls.add(val);
19         hm.put(val, ls.size()-1);
20         return true;
21     }
22     
23     /** Removes a value from the set. Returns true if the set contained the specified element. */
24     public boolean remove(int val) {
25         if(!hm.containsKey(val)){
26             return false;
27         }
28         
29         int ind = hm.get(val);
30         //swap当前数字和ls的最后一个数字,方便在list上remove当前数字时不影响其他数字index
31         if(ind < ls.size()-1){
32             int lastElement = ls.get(ls.size()-1);
33             ls.set(ind, lastElement);
34             hm.put(lastElement, ind);
35         }
36         
37         hm.remove(val);
38         ls.remove(ls.size()-1);
39         return true;
40     }
41     
42     /** Get a random element from the set. */
43     public int getRandom() {
44         return ls.get(rand.nextInt(ls.size()));
45     }
46 }
47 
48 /**
49  * Your RandomizedSet object will be instantiated and called as such:
50  * RandomizedSet obj = new RandomizedSet();
51  * boolean param_1 = obj.insert(val);
52  * boolean param_2 = obj.remove(val);
53  * int param_3 = obj.getRandom();
54  */

 跟上Insert Delete GetRandom O(1) - Duplicates allowed.

LeetCode Insert Delete GetRandom O(1)