-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
37 lines (34 loc) · 974 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
public class Solution {
int[] init;
public Solution(int[] nums) {
init=nums;
}
/** Resets the array to its original configuration and return it. */
public int[] reset() {
return init;
}
/** Returns a random shuffling of the array. */
public int[] shuffle() {
int[] random=new int[init.length];
for (int i = 0; i < random.length; i++) {
random[i]=i;
}
Random r=new Random();
for (int i = random.length-1; i >= 0 ; i--) {
int t=r.nextInt(i+1);
int swap=random[i];
random[i]=random[t];
random[t]=swap;
}
for (int i = 0; i < random.length; i++) {
random[i]=init[random[i]];
}
return random;
}
}
/**
* 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();
*/