-
Notifications
You must be signed in to change notification settings - Fork 22
/
Shuffle an Array.java
executable file
·165 lines (138 loc) · 4.18 KB
/
Shuffle an Array.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
M
1524032851
tags: Permutation
像shuffle music 一样, 做一套shuffle array的functions:
shuffle() 给出random的permutation
reset() 给出最初的nums
#### Permutation
- Permutation 实际上就是在list/array/... 上面给元素换位置
- 硬换位置, 每次换的位置不同, 用random来找到要换的index
- 维持同一个random seed
- O(n)
##### Note
- compute all permutations 太慢, 不可行.
```
/*
Shuffle a set of numbers without duplicates.
Example:
// Init an array with set 1, 2, and 3.
int[] nums = {1,2,3};
Solution solution = new Solution(nums);
// Shuffle the array [1,2,3] and return its result.
Any permutation of [1,2,3] must equally likely to be returned.
solution.shuffle();
// Resets the array back to its original configuration [1,2,3].
solution.reset();
// Returns the random shuffling of array [1,2,3].
solution.shuffle();
*/
/*
Thoughts:
1. Store original = nums;
2. Use universal Random object
3. Randomly pick n different indexes to swap around, which produces permutation
*/
class Solution {
private int[] original;
private Random random;
public Solution(int[] nums) {
if (nums == null || nums.length == 0) {
original = new int[]{ };
return;
}
int n = nums.length;
original = Arrays.copyOf(nums, n);
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 || original.length == 0) {
return original;
}
int n = original.length;
int[] output = Arrays.copyOf(original, n);
for (int i = 0; i < n; i++) {
int offset = random.nextInt(n - i);
swap(output, i, i + offset);
}
return output;
}
private void swap(int[] nums, int x, int y) {
int temp = nums[y];
nums[y] = nums[x];
nums[x] = 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();
*/
/*
It's not necessary to compute all permutations during initialization, not efficient.
Thoughts:
1. Store a original versiion
2. Find all permutations
3. Return a random item from the list
*/
class Solution {
private int[] original;
private List<int[]> permutation;
public Solution(int[] nums) {
if (nums == null || nums.length == 0) {
original = null;
permutation = null;
}
int n = nums.length;
// Copy
original = Arrays.copyOf(nums, n);
// Permutation
Arrays.sort(nums);
permutation = new ArrayList<>();
permutation.add(nums);
for (int pos = 0; pos < n; pos++) {
for (int i = permutation.size() - 1; i >= 0; i--) {
int[] arr = permutation.get(i);
Arrays.sort(arr, pos, n);
for (int j = pos + 1; j < n; j++) {
if (arr[j] == arr[j - 1]) {
continue;
}
swap(arr, pos, j);
permutation.add(Arrays.copyOf(arr, n));
swap(arr, pos, j);
}
}
}
}
private void swap(int[] nums, int x, int y) {
int temp = nums[y];
nums[y] = nums[x];
nums[x] = temp;
}
/** 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 (permutation == null || permutation.size() == 0 || original == null || original.length == 0) {
return null;
}
Random rd = new Random();
int index = rd.nextInt(original.length);
return permutation.get(index);
}
}
/**
* 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();
*/
```