-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPermute.java
42 lines (36 loc) · 1.06 KB
/
Permute.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
package ds.backtrack;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* 全排列
* LeetCode 46 https://leetcode-cn.com/problems/permutations/
*
* @author yangyi 2020年12月17日11:13:47
*/
public class Permute {
private List<List<Integer>> result = new LinkedList<>();
public List<List<Integer>> permute(int[] nums) {
LinkedList<Integer> track = new LinkedList<>();
backtrack(track, nums);
return result;
}
public void backtrack(LinkedList<Integer> track, int[] nums) {
if (track.size() == nums.length) {
result.add(new LinkedList<>(track));
return;
}
for (int num : nums) {
if (track.contains(num)) {
continue;
}
track.add(num);
backtrack(track, nums);
track.removeLast();
}
}
public static void main(String[] args) {
Permute permute = new Permute();
System.out.println(Arrays.toString(permute.permute(new int[]{1, 2, 3}).toArray()));
}
}