forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
36 lines (31 loc) · 833 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
class Solution {
private int[] nums;
private int[] original;
private Random rand;
public Solution(int[] nums) {
this.nums = nums;
this.original = Arrays.copyOf(nums, nums.length);
this.rand = new Random();
}
public int[] reset() {
nums = Arrays.copyOf(original, original.length);
return nums;
}
public int[] shuffle() {
for (int i = 0; i < nums.length; ++i) {
swap(i, i + rand.nextInt(nums.length - i));
}
return nums;
}
private void swap(int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
}
/**
* 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();
*/