我试过这样做:
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]]
。这里怎么了?
将rst.append(list)更改为rst.apped(copy.deepcopy(list))后,问题得到了解决。