forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
38 lines (31 loc) · 980 Bytes
/
Solution.java
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
package g0301_0400.s0384_shuffle_an_array;
// #Medium #Array #Math #Randomized #Algorithm_II_Day_20_Others
// #2022_07_13_Time_52_ms_(91.77%)_Space_48.2_MB_(92.20%)
import java.util.Random;
@SuppressWarnings("java:S2245")
public class Solution {
private int[] nums;
private Random random;
public Solution(int[] nums) {
this.nums = nums;
this.random = new Random();
}
// Resets the array to its original configuration and return it.
public int[] reset() {
return this.nums;
}
// Returns a random shuffling of the array.
public int[] shuffle() {
int[] shuffled = this.nums.clone();
for (int i = nums.length - 1; i > 0; i--) {
int j = random.nextInt(i + 1);
swap(shuffled, i, j);
}
return shuffled;
}
private void swap(int[] shuffled, int i, int j) {
int tmp = shuffled[i];
shuffled[i] = shuffled[j];
shuffled[j] = tmp;
}
}