我试着这样做:
def search(rst,list,nums):
i = 0
if len(list) == len(nums):
rst.append(list)
return
while i < len(nums):
if nums[i] not in list:
list.append(nums[i])
print i,list,rst
search(rst,list,nums)
list.pop()
i += 1
return rst
def permutation(nums):
rst = []
list = []
if nums == None or len(nums) == 0:
return rst
search(rst,list,nums)
return rst
rst
的最终值不正确。最初rst
的值为[[1,2,3]]
,这是正确的,但rst
的值已更改为[[1,3,2],[1,3,2]]
。这有什么不对?
答案 0 :(得分:0)
将rst.append(list)更改为rst.append(copy.deepcopy(list))后,问题得以解决。