-
Notifications
You must be signed in to change notification settings - Fork 0
/
380. 常数时间插入、删除和获取随机元素.js
56 lines (51 loc) · 1.49 KB
/
380. 常数时间插入、删除和获取随机元素.js
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
// 方法一 数组 + Map
var RandomizedSet = function () {
this.data = []
this.keys = new Map()
};
RandomizedSet.prototype.insert = function (val) {
if (this.keys.get(val) !== undefined) {
return false
}
this.keys.set(val, this.data.length)
this.data.push(val)
return true
};
RandomizedSet.prototype.remove = function (val) {
let index = this.keys.get(val)
if (index === undefined) return false
let temp = this.data[index]
this.data[index] = this.data[this.data.length - 1]
this.data[this.data.length - 1] = temp
this.keys.set(this.data[index], index)
this.data.pop()
this.keys.delete(val)
return true
};
RandomizedSet.prototype.getRandom = function () {
let i = Math.random() * this.data.length | 0
return this.data[i]
};
// 方法二 Set方法getRandom时解构为数组取值
var obj = new RandomizedSet()
var param_1 = obj.insert(1)
var param_2 = obj.remove(2)
var param_3 = obj.insert(2)
var param_4 = obj.getRandom()
var param_5 = obj.remove(1)
var param_6 = obj.insert(2)
var param_7 = obj.getRandom()
// var param_1 = obj.insert(0)
// var param_2 = obj.insert(1)
// var param_3 = obj.remove(0)
// var param_4 = obj.insert(2)
// var param_5 = obj.remove(1)
// var param_6 = obj.getRandom()
console.log(param_1, param_2, param_3, param_4, param_5, param_6, param_7)
/**
* Your RandomizedSet object will be instantiated and called as such:
* var obj = new RandomizedSet()
* var param_1 = obj.insert(val)
* var param_2 = obj.remove(val)
* var param_3 = obj.getRandom()
*/