leetcode-380-Insert-Delete-GetRandom-O(1)

描述


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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 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();

分析


这道题要求我们实现一个操作数组的 API,比较特别的一点是插入,删除,获取随机数这样的操作的复杂度是 $O(1)$,要解决这个问题,我们需要用到额外的空间复杂度。可以用数组来存储数据,用哈希表来存储数据与索引之间的关系,插入很好解决,直接在数组末尾添加数据,哈希表增加一条数据即可,时间复杂度是常数,对于删除操作,每次删除时,先根据哈希表获得要删除的值在数组中的位置,然后将数组中该位置对应的值与数组末尾的值进行交互,接着删除数组末尾的值,删除哈希表中的数据即可,时间复杂度也是常数,这样就通过空间交换时间的方式的解决了这个问题。

下一题 381. Insert Delete GetRandom O(1) - Duplicates allowed 允许插入重复的值更有意思。

解决方案1(Java)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class RandomizedSet {
private ArrayList<Integer> nums;
private HashMap<Integer, Integer> hashIndexMap;
private java.util.Random rand = new java.util.Random();

/** Initialize your data structure here. */
public RandomizedSet() {
this.nums = new ArrayList<Integer>();
this.hashIndexMap = new HashMap<Integer, Integer>();
}

/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
public boolean insert(int val) {
boolean hasKey = hashIndexMap.containsKey(val);
if (hasKey) {
return false;
}
this.hashIndexMap.put(val, nums.size());
this.nums.add(val);
return true;
}

/** Removes a value from the set. Returns true if the set contained the specified element. */
public boolean remove(int val) {
boolean hasKey = hashIndexMap.containsKey(val);
if (!hasKey) {
return false;
}
int index = hashIndexMap.get(val);
int lastNum = nums.get(nums.size()-1);
this.nums.set(index, lastNum);
this.hashIndexMap.put(lastNum, index);

this.hashIndexMap.remove(val);
this.nums.remove(nums.size()-1);
return true;
}

/** Get a random element from the set. */
public int getRandom() {
return this.nums.get(rand.nextInt(nums.size()));
}
}

/**
* Your RandomizedSet object will be instantiated and called as such:
* RandomizedSet obj = new RandomizedSet();
* boolean param_1 = obj.insert(val);
* boolean param_2 = obj.remove(val);
* int param_3 = obj.getRandom();
*/

相关问题


题目来源