-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShuffle an array.java
More file actions
35 lines (32 loc) · 992 Bytes
/
Copy pathShuffle an array.java
File metadata and controls
35 lines (32 loc) · 992 Bytes
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
class Solution {
int[] original = null;
int[] shuffle = null;
Random rand = null;
public Solution(int[] nums) {
original = nums;
shuffle = Arrays.copyOf(nums, nums.length);
rand = new Random();
}
/** Resets the array to its original configuration and return it. */
public int[] reset() {
//shuffle = Arrays.copyOf(original, original.length);
return original;
}
/** Returns a random shuffling of the array. */
public int[] shuffle() {
for(int i = 0; i<shuffle.length; i++)
{
int randomPosition = rand.nextInt(shuffle.length);
int temp = shuffle[i];
shuffle[i] = shuffle[randomPosition];
shuffle[randomPosition] = temp;
}
return shuffle;
}
}
/**
* 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();
*/