-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermute.java
More file actions
49 lines (43 loc) · 1.43 KB
/
Permute.java
File metadata and controls
49 lines (43 loc) · 1.43 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
46
47
48
49
package com.leetcode.backtracking;
import java.util.ArrayList;
import java.util.List;
/**
* @author: yhl
* @DateTime: 2021/2/26 15:44
* @Description:全排列
*/
public class Permute {
public static void main(String[] args) {
int[] nums = {1, 2, 3};
Permute permute = new Permute();
List<List<Integer>> result = permute.permute(nums);
System.out.println(result);
}
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums.length == 0) {
return res;
}
List<Integer> path = new ArrayList<>();
int length = nums.length;
boolean[] used = new boolean[length];
dfs(nums, path, res, used);
return res;
}
private void dfs(int[] nums, List<Integer> path, List<List<Integer>> res, boolean[] used) {
if (path.size() == nums.length) {
res.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < nums.length; i++) {
if (!used[i]) {
path.add(nums[i]);
used[i] = true;
dfs(nums, path, res, used);
// 注意:下面这两行代码发生 「回溯」,回溯发生在从 深层结点 回到 浅层结点 的过程,代码在形式上和递归之前是对称的
used[i] = false;
path.remove(path.size() - 1);
}
}
}
}