forked from kdn251/interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermutations.java
34 lines (31 loc) · 882 Bytes
/
Permutations.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
//Given a collection of distinct numbers, return all possible permutations.
//
//For example,
//[1,2,3] have the following permutations:
//[
//[1,2,3],
//[1,3,2],
//[2,1,3],
//[2,3,1],
//[3,1,2],
//[3,2,1]
//]
class Permutations {
public List<List<Integer>> permute(int[] nums) {
LinkedList<List<Integer>> result = new LinkedList<List<Integer>>();
result.add(new ArrayList<Integer>());
for (int n: nums) {
int size = result.size();
while(size > 0) {
List<Integer> current = result.pollFirst();
for (int i = 0; i <= current.size(); i++) {
List<Integer> temp = new ArrayList<Integer>(current);
temp.add(i, n);
result.add(temp);
}
size--;
}
}
return result;
}
}