File tree 1 file changed +36
-0
lines changed
solution/384.Shuffle an Array
1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ */
4
+ const Solution = function ( nums ) {
5
+ this . nums = nums || [ ] ;
6
+ } ;
7
+
8
+ /**
9
+ * Resets the array to its original configuration and return it.
10
+ * @return {number[] }
11
+ */
12
+ Solution . prototype . reset = function ( ) {
13
+ return this . nums ;
14
+ } ;
15
+
16
+ /**
17
+ * Returns a random shuffling of the array.
18
+ * @return {number[] }
19
+ */
20
+ Solution . prototype . shuffle = function ( ) {
21
+ let a = this . nums . slice ( ) ;
22
+ for ( let i = 0 ; i < a . length ; i ++ ) {
23
+ let rand = Math . floor ( Math . random ( ) * ( a . length - i ) ) + i ;
24
+ let tmp = a [ i ] ;
25
+ a [ i ] = a [ rand ] ;
26
+ a [ rand ] = tmp ;
27
+ }
28
+ return a ;
29
+ } ;
30
+
31
+ /**
32
+ * Your Solution object will be instantiated and called as such:
33
+ * var obj = Object.create(Solution).createNew(nums)
34
+ * var param_1 = obj.reset()
35
+ * var param_2 = obj.shuffle()
36
+ */
You can’t perform that action at this time.
0 commit comments