-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomizedSet.js
More file actions
59 lines (53 loc) · 1.6 KB
/
RandomizedSet.js
File metadata and controls
59 lines (53 loc) · 1.6 KB
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
52
53
54
55
56
57
58
59
// 设计一个支持在平均 时间复杂度 O(1) 下,执行以下操作的数据结构。
// insert(val):当元素 val 不存在时,向集合中插入该项。
// remove(val):元素 val 存在时,从集合中移除该项。
// getRandom:随机返回现有集合中的一项。每个元素应该有相同的概率被返回。
// getRandom的时候哈希表转数组时间复杂度为O(N),因此需要一个辅助数组储存数据。
var RandomizedSet = function () {
this.map = new Map();
this.arr = [];
};
/**
* Inserts a value to the set. Returns true if the set did not already contain the specified element.
* @param {number} val
* @return {boolean}
*/
RandomizedSet.prototype.insert = function (val) {
if (!this.map.has(val)) {
this.map.set(val, this.arr.length);
this.arr.push(val);
return true;
} else {
return false;
}
};
/**
* Removes a value from the set. Returns true if the set contained the specified element.
* @param {number} val
* @return {boolean}
*/
RandomizedSet.prototype.remove = function (val) {
if (this.map.has(val)) {
let index = this.map.get(val);
if (index === this.arr.length - 1) {
this.arr.pop();
this.map.delete(val);
} else {
let lastVal = this.arr.pop();
this.arr[index] = lastVal;
this.map.set(lastVal, index);
this.map.delete(val);
}
return true;
} else {
return false;
}
};
/**
* Get a random element from the set.
* @return {number}
*/
RandomizedSet.prototype.getRandom = function () {
let len = this.arr.length;
return this.arr[Math.floor(Math.random() * len)];
};