给出一个列表[1,2,3],其全排列为:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
和子集的差别就是一个start,一个是visited数组
https://www.lintcode.com/zh-cn/problem/permutations/
def subjectHelper(self, nums, visited):
if len(self.result) == len(nums):
self.resultAll.append(list(self.result))
for i in range(len(nums)):
if visited[i]:
continue
self.result.append(nums[i])
visited[i] = True
self.subjectHelper(nums, visited)
visited[i] = False
self.result.pop(-1)
def permute(self, nums):
# write your code here
n = len(nums)
if n == 0:
return []
self.result = []
self.resultAll = []
self.subjectHelper(nums, [False]*n)
return self.resultAll
网友评论