Skip to content

Commit e233944

Browse files
committed
Create 17. 全排列.md
1 parent bd0a77a commit e233944

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

17. 全排列.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
**给定一个不含重复数字的数组 nums ,返回其所有可能的全排列 。你可以按任意顺序返回答案。**
2+
3+
```
4+
输入:nums = [1,2,3]
5+
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
6+
```
7+
8+
```
9+
class Solution:
10+
def permute(self, nums: List[int]) -> List[List[int]]:
11+
n = len(nums)
12+
res = []
13+
#当前组合
14+
cur = []
15+
#构造递归
16+
def dfs(cur):
17+
#递归终止条件
18+
if len(cur) == n:
19+
res.append(cur)
20+
return
21+
for num in nums:
22+
if num not in cur:
23+
dfs(cur+[num])
24+
#调用递归
25+
dfs(cur)
26+
#返回要优化的目标
27+
return res
28+
```

0 commit comments

Comments
 (0)