File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ var RandomizedSet = function ( ) {
2+ this . set = new Set ( ) ;
3+ } ;
4+
5+ /**
6+ * @param {number } val
7+ * @return {boolean }
8+ */
9+ RandomizedSet . prototype . insert = function ( val ) {
10+ if ( ! this . set . has ( val ) ) {
11+ this . set . add ( val ) ;
12+ return true ;
13+ } else {
14+ return false ;
15+ }
16+ } ;
17+
18+ /**
19+ * @param {number } val
20+ * @return {boolean }
21+ */
22+ RandomizedSet . prototype . remove = function ( val ) {
23+ return this . set . delete ( val ) ;
24+ } ;
25+
26+ /**
27+ * @return {number }
28+ */
29+ RandomizedSet . prototype . getRandom = function ( ) {
30+ const keys = Array . from ( this . set . keys ( ) ) ;
31+ const seed = Math . floor ( Math . random ( ) * keys . length ) ;
32+ return keys [ seed ] ;
33+ } ;
34+
35+ /**
36+ * Your RandomizedSet object will be instantiated and called as such:
37+ * var obj = new RandomizedSet()
38+ * var param_1 = obj.insert(val)
39+ * var param_2 = obj.remove(val)
40+ * var param_3 = obj.getRandom()
41+ */
You can’t perform that action at this time.
0 commit comments