forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShuffleAnArray.java
90 lines (75 loc) · 2.87 KB
/
ShuffleAnArray.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
package com.fishercoder.solutions;
/**384. Shuffle an Array
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();*/
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Random;
public class ShuffleAnArray {
public static void main(String...strings){
int[] nums = new int[]{1,2,3};
Solution_for_this_question test = new Solution_for_this_question(nums);
}
}
class Solution_for_this_question {
//Note: the problem states that this is a set without duplicates which makes building all combinations easier
private List<List<Integer>> combinations;
private int[] original;
private Random random;
public Solution_for_this_question(int[] nums) {
original = nums;
random = new Random();
combinations = buildAllComb(nums);
}
//insert next value into all possible positions, I wrote this method myself, of course it could be simplified to not use a queue
//but it just naturally came into my mind that I used a queue
private List<List<Integer>> buildAllComb(int[] nums) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if(nums == null || nums.length == 0) return result;
List<Integer> list = new ArrayList<Integer>();
list.add(nums[0]);
Queue<List<Integer>> q = new LinkedList();
q.offer(list);
for(int i = 1; i < nums.length; i++){
int qSize = q.size();
for(int k = 0; k < qSize; k++){
List<Integer> currList = q.poll();
for(int j = 0; j <= currList.size(); j++){
List<Integer> newL = new ArrayList<Integer>(currList);
newL.add(j, nums[i]);
q.offer(newL);
}
}
}
while(!q.isEmpty()){
result.add(q.poll());
}
return result;
}
/** 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 randomIndex = random.nextInt(combinations.size());
List<Integer> list = combinations.get(randomIndex);
int[] result = new int[list.size()];
for(int i = 0; i < list.size(); i++){
result[i] = list.get(i);
}
return result;
}
}