标签:没有 lan for enum app etc end set 数字
给定一个 没有重复 数字的序列,返回其所有可能的全排列。
示例:
输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
if len(nums) <= 1: # 递归终止条件
return [nums]
res = []
for idx, num in enumerate(nums):
res_nums = nums[:idx] + nums[idx + 1:] # 确定剩余元素
for j in self.permute(res_nums):
res.append([num] + j)
return res
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
ans = []
cur = []
visit = set()
self.permute_solver(nums, ans, cur, visit)
return ans
def permute_solver(self,nums,ans,cur,visit):
if len(cur)==len(nums):
ans.append(cur[:])
return
for i in range(len(nums)):
if nums[i] in visit:
continue
cur.append(nums[i])
visit.add(nums[i])
self.permute_solver(nums,ans,cur,visit)
num = cur.pop()
visit.remove(num)
return
标签:没有 lan for enum app etc end set 数字
原文地址:https://www.cnblogs.com/sandy-t/p/13285404.html