-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutation.java
More file actions
41 lines (37 loc) · 1.2 KB
/
Copy pathPermutation.java
File metadata and controls
41 lines (37 loc) · 1.2 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
package LeetCode;
import java.util.ArrayList;
import java.util.List;
/**
* @FileName: Permutation.java
* @Description: 全排列
* @Author: ABCpril
* @Date: 2021/12/03
*/
public class Permutation {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums.length == 0) return res;
dfs(nums, res, new ArrayList<>(), new boolean[nums.length]);
return res;
}
// 1.递归的含义:以permutation开头的排列,如果长度达到 n,就加入res
private void dfs(int[] nums, List<List<Integer>> res, List<Integer> permutation, boolean[] visited) {
// 3.递归的出口:排列长度达到 n
if (permutation.size() == nums.length) {
res.add(new ArrayList<>(permutation));
return;
}
// 2.递归的拆解
for (int i = 0; i < nums.length; i++) {
if (visited[i]) {
continue;
}
permutation.add(nums[i]);
visited[i] = true;
dfs(nums, res, permutation, visited);
// 回溯
visited[i] = false;
permutation.remove(permutation.size() - 1);
}
}
}