Skip to content

Commit b752282

Browse files
add solution for 384 in js
1 parent e5f4da8 commit b752282

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
*/

0 commit comments

Comments
 (0)