import copy
class Solution:
def helper(self, nums, tmp, ans):
if len(tmp) == len(nums):
# 注意这里的copy方式. 如果只是写成append(tmp), 会是个空结果, 因为tmp最后为空, 坑!
ans.append(copy.copy(tmp))
return ans
for i in range(len(nums)):
if nums[i] not in tmp:
tmp.append(nums[i])
self.helper(nums, tmp, ans)
tmp.pop()
return ans
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
tmp = list()
ans = []
self.helper(nums, tmp, ans)
return ans
Leetcode46. Permutations
最新推荐文章于 2024-04-06 00:16:51 发布