-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.py
More file actions
35 lines (30 loc) · 1.13 KB
/
Solution.py
File metadata and controls
35 lines (30 loc) · 1.13 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
from typing import List
from itertools import permutations
class Solution:
# 使用自带工具函数permutations进行序列
def permute(self, nums: List[int]) -> List[List[int]]:
return permutations(list(nums))
# 递归解法
def permute1(self, nums: List[int]) -> List[List[int]]:
def dfs(nums: List[int], res: List[int], path: List[int]):
if not nums:
res.append(path)
else:
for i in range(len(nums)):
dfs(nums[:i] + nums[i+1:], res, path + [nums[i]])
res = []
dfs(nums, res, [])
return res
# 交换数的位置
def permute2(self, nums: List[int]) -> List[List[int]]:
def sort(nums, res: List[int], start):
if start == len(nums):
res.append(nums[:]) # 需深拷贝
return
for i in range(start, len(nums)):
nums[start], nums[i] = nums[i], nums[start]
sort(nums, res, start + 1)
nums[start], nums[i] = nums[i], nums[start]
res = []
sort(nums, res, 0)
return res