-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSolution.java
More file actions
45 lines (38 loc) · 1.21 KB
/
Solution.java
File metadata and controls
45 lines (38 loc) · 1.21 KB
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
package Permutations;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* User: Danyang
* Date: 1/20/2015
* Time: 11:35
*
* Given a collection of 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], and [3,2,1].
*/
public class Solution {
public List<List<Integer>> permute(int[] num) {
List<List<Integer>> ret = new ArrayList<>();
dfs(IntStream.of(num).boxed().collect(Collectors.toList()), new ArrayList<>(), ret);
return ret;
}
void dfs(List<Integer> seq, List<Integer> cur, List<List<Integer>> ret) {
if(seq.size()==0)
ret.add(new ArrayList<>(cur));
// list manipulation is verbose
for(int i=0; i<seq.size(); i++) {
List<Integer> next = new ArrayList<>(seq);
int t = next.remove(i);
cur.add(t);
dfs(next, cur, ret);
cur.remove(cur.size()-1);
}
}
public static void main(String[] args) {
List<List<Integer>> ret = new Solution().permute(new int[]{0, 1});
System.out.println(ret);
}
}