Skip to content

Commit 9591718

Browse files
committed
Add solution for LC 382
1 parent 6d46d59 commit 9591718

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

384. Shuffle an Array.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,22 @@ Constraints:
2424
- At most 10,000 calls in total will be made to reset and shuffle.
2525
2626
*/
27+
28+
class Solution {
29+
constructor(nums) {
30+
this.nums = nums;
31+
}
32+
33+
reset = () => this.nums;
34+
35+
shuffle = () => {
36+
let shuffled = [...this.nums];
37+
38+
for (let i = shuffled.length - 1; i > 0; i--) {
39+
const j = Math.floor(Math.random() * (i + 1));
40+
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
41+
}
42+
43+
return shuffled;
44+
};
45+
}

0 commit comments

Comments
 (0)