-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0046.java
More file actions
44 lines (40 loc) · 1.15 KB
/
Copy pathLeetCode0046.java
File metadata and controls
44 lines (40 loc) · 1.15 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
/* Permutations
* Input: [1,2,3]
* Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
* */
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class LeetCode0046 {
public static void main(String args[]) {
int[] candidates = {1,2,3};
System.out.println(permute(candidates));
}
public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
LinkedList<Integer> inter = new LinkedList<Integer>();
backtrack(inter, 0, nums, res);
return res;
}
public static void backtrack(LinkedList<Integer> inter, int start, int[] nums, List<List<Integer>> res){
if (inter.size() == nums.length){
res.add(new ArrayList<Integer>(inter));
return;
}
for (int i = start; i < nums.length; i++) {
if (inter.contains(nums[i]))
continue;
inter.add(nums[i]);
backtrack(inter, start, nums, res);
inter.removeLast();
}
}
}