-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathSolution46.java
52 lines (42 loc) · 1.74 KB
/
Solution46.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
43
44
45
46
47
48
49
50
51
52
package backstracking_problem;
import java.util.ArrayList;
import java.util.List;
/**
* 排列问题
* 时间复杂度:O(n*n!)
* 空间复杂度:O(n)
*/
public class Solution46 {
public List<List<Integer>> permute(int[] nums) {
// 1、创建一个组合嵌套列表 & 组合列表 & 记录已访问元素的数组
List<List<Integer>> permutes = new ArrayList<>();
List<Integer> permuteList = new ArrayList<>();
boolean[] visited = new boolean[nums.length];
// 2、回溯
backtracking(permuteList, permutes, visited, nums);
return permutes;
}
// 1,2,3 => 移除3,2 => 1,3,2 => 移除 2,3,1 => 2,1,3 => ...
private void backtracking(List<Integer> permuteList, List<List<Integer>> permutes, boolean[] visited, int[] nums) {
// 1、如果当前组合等于目标数组长度,则添加到嵌套列表中并返回到上一层
if (permuteList.size() == nums.length) {
permutes.add(new ArrayList<>(permuteList));
return;
}
// 2、遍历已访问数组,如果当前元素没有被访问过,则添加到记录组合列表中,并继续下一层回溯,
// 回溯到底添加完组合列表后,则删除 当前 组合列表中最后一个元素
for (int i = 0; i < visited.length; i++) {
if (!visited[i]) {
visited[i] = true;
permuteList.add(nums[i]);
backtracking(permuteList, permutes, visited, nums);
permuteList.remove(permuteList.size() - 1);
visited[i] = false;
}
}
}
public static void main(String[] args) {
int[] nums = new int[]{1, 2, 3};
new Solution46().permute(nums);
}
}