-
Notifications
You must be signed in to change notification settings - Fork 0
/
380.js
54 lines (49 loc) · 1.32 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
/**
* Initialize your data structure here.
*/
var RandomizedSet = function() {
this.index = {};
this.data={};
this.length = 0;
};
/**
* 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.index[val]!==undefined) return false;
let idx = this.length;
this.index[val]=idx;
this.data[idx] = val;
this.length++;
return true;
};
/**
* 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.index[val]!==undefined){
let lastIdx = this.length-1;
//exchange target with last & delete target
let tempIdx = this.index[val];
let lastData = this.data[lastIdx]
this.index[lastData] = tempIdx;
this.data[tempIdx] = lastData;
delete this.index[val];
delete this.data[lastIdx];
this.length--;
return true;
}
return false;
};
/**
* Get a random element from the set.
* @return {number}
*/
RandomizedSet.prototype.getRandom = function() {
let ran = Math.floor(Math.random()*this.length);
return this.data[ran];
};