forked from xiaoyaoworm/Leetcode-java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path384_shuffleArray.java
96 lines (82 loc) · 2.69 KB
/
384_shuffleArray.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//copy 一个数组可以用clone()!!!
//principle is swap : for loop: i = 1~len-1 with j=random.nextInt(i+1)
public class Solution {
int[] original;
Random random;
public Solution(int[] nums) {
this.original = nums;
this.random = new Random();
}
/** 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() {
if(original == null) return null;
int[] copy = original.clone(); //Attention 1: you can use arr.clone() to make a clone!!!!
for(int i = 1; i < copy.length; i++){ //Principle is here, i can be 1~copy.length()-1
int j = random.nextInt(i+1); //j can be any number between 0~i(including)
swap(copy, i, j); //swap them in for loop.
}
return copy;
}
public void swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
/**
* 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();
*/
//TLE解法,有个问题就是,我是initial的时候,把所有的permutations都列出来了。
//其实并不需要
public class Solution {
int[] original;
List<int[]> permutations;
public Solution(int[] nums) {
this.original = nums;
this.permutations = new ArrayList<int[]>();
dfs(nums, permutations, 0);
}
/** 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() {
Random random = new Random();
int i = random.nextInt(permutations.size());
return permutations.get(i);
}
public void dfs(int[] nums, List<int[]> permutations, int pos){
if(pos == nums.length){
int[] copy = new int[nums.length];
for(int i = 0; i < nums.length; i++){
copy[i] = nums[i];
}
permutations.add(copy);
return;
}
for(int i = pos; i < nums.length; i++){
swap(nums, pos, i);
dfs(nums, permutations, i+1);
swap(nums, i, pos);
}
}
public void swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
/**
* 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();
*/