You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
582 B
35 lines
582 B
package class34;
|
|
|
|
public class Problem_0384_ShuffleAnArray {
|
|
|
|
class Solution {
|
|
private int[] origin;
|
|
private int[] shuffle;
|
|
private int N;
|
|
|
|
public Solution(int[] nums) {
|
|
origin = nums;
|
|
N = nums.length;
|
|
shuffle = new int[N];
|
|
for (int i = 0; i < N; i++) {
|
|
shuffle[i] = origin[i];
|
|
}
|
|
}
|
|
|
|
public int[] reset() {
|
|
return origin;
|
|
}
|
|
|
|
public int[] shuffle() {
|
|
for (int i = N - 1; i >= 0; i--) {
|
|
int r = (int) (Math.random() * (i + 1));
|
|
int tmp = shuffle[r];
|
|
shuffle[r] = shuffle[i];
|
|
shuffle[i] = tmp;
|
|
}
|
|
return shuffle;
|
|
}
|
|
}
|
|
|
|
}
|