Permutations

全排列

题目介绍

全排列

给定一个 没有重复 数字的序列,返回其所有可能的全排列。

示例:

1
2
3
4
5
6
7
8
9
10
输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

题目解法

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
package algorithm;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Permutations {

public static List<List<Integer>> permute(int[] nums) {

List<Integer> output = new ArrayList<>();
for (int num : nums) {
output.add(num);
}
List<List<Integer>> ans = new ArrayList<>();
backtrack(output, ans, 0);
return ans;
}

public static void backtrack(List<Integer> output, List<List<Integer>> ans, int first) {
if (first == output.size()) {
ans.add(new ArrayList<>(output));
}
for (int i = first; i < output.size(); i++) {
Collections.swap(output, i, first);
backtrack(output, ans, first + 1);
Collections.swap(output, i, first);
}
}

public static void main(String[] args) {
int[] nums = {1,2,3};
System.out.println(permute(nums));
}
}

打印:

1
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 2, 1], [3, 1, 2]]

思路:

思路上,使用回溯。


Permutations
https://yangtzeshore.github.io/2021/03/09/Permutations/
作者
Chen Peng
发布于
2021年3月9日
许可协议