-
Notifications
You must be signed in to change notification settings - Fork 1
/
384. Shuffle an Array
41 lines (37 loc) · 1.01 KB
/
384. Shuffle an Array
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
39
40
41
import java.util.Random;
class Solution {
int[] original;
int[] copy;
Random rand;
public Solution(int[] nums) {
original = new int[nums.length];
copy = new int[nums.length];
rand = new Random();
for(int i=0;i<nums.length;i++){
original[i] = nums[i];
copy[i] = nums[i];
}
}
/** Resets the array to its original configuration and return it. */
public int[] reset() {
return original;
}
/** Returns a random shuffling of the array. */
public int[] shuffle() {
int len = original.length-1;
while(len>=0){
int index = rand.nextInt(original.length);
int temp = copy[index];
copy[index] = copy[len];
copy[len] = temp;
len--;
}
return copy;
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* int[] param_1 = obj.reset();
* int[] param_2 = obj.shuffle();
*/