leetcode46 Permutations-zh
# 46. 全排列 (opens new window)
English Version (opens new window)
# 题目描述
给定一个 没有重复 数字的序列,返回其所有可能的全排列。
示例:
输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ]
# 解法
# Python3
回溯法:
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
def dfs(nums, i, res, path, used):
if i == len(nums):
res.append(copy.deepcopy(path))
return
for j in range(len(nums)):
if not used[j]:
path.append(nums[j])
used[j] = True
dfs(nums, i + 1, res, path, used)
used[j] = False
path.pop()
res, path = [], []
used = [False] * len(nums)
dfs(nums, 0, res, path, used)
return res
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
切分数组:
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
if len(nums) <= 1:
return [nums]
res = []
for i, num in enumerate(nums):
n = nums[:i] + nums[i + 1:]
for item in self.permute(n):
res.append([num] + item)
return res
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# Java
回溯法:
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
boolean[] used = new boolean[nums.length];
dfs(nums, 0, res, path, used);
return res;
}
private void dfs(int[] nums, int i, List<List<Integer>> res, List<Integer> path, boolean[] used) {
if (i == nums.length) {
res.add(new ArrayList<>(path));
return;
}
for (int j = 0; j < nums.length; ++j) {
if (!used[j]) {
path.add(nums[j]);
used[j] = true;
dfs(nums, i + 1, res, path, used);
used[j] = false;
path.remove(path.size() - 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
- 递归:
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
permute(res, nums, 0);
return res;
}
private void permute(List<List<Integer>> res, int[] nums, int start) {
if (start == nums.length) {
List<Integer> t = new ArrayList<>();
for (int e : nums) {
t.add(e);
}
res.add(t);
return;
}
for (int i = start; i < nums.length; ++i) {
swap(nums, i, start);
permute(res, nums, start + 1);
swap(nums, i, start);
}
}
private void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
}
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
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
# JavaScript
/**
* @param {number[]} nums
* @return {number[][]}
*/
var permute = function(nums) {
let res = [];
let solution = [];
let record = new Array(nums.length).fill(false);
dfs(nums, 0, record, solution, res);
return res;
};
function dfs (nums, depth, record, solution, res) {
if (depth == nums.length) {
res.push(solution.slice());
return;
}
for (let i = 0; i < nums.length; i++) {
if (!record[i]) {
solution.push(nums[i]);
record[i] = true;
dfs(nums, depth + 1, record, solution, res);
solution.pop();
record[i] = false;
}
}
}
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
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
# ...
1
编辑 (opens new window)
上次更新: 2021/10/30, 12:58:38