lintcode置换 - 输出不正确

时间:2016-05-10 06:56:02

标签: python

我试着这样做:

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]]。这有什么不对?

1 个答案:

答案 0 :(得分:0)

将rst.append(list)更改为rst.append(copy.deepcopy(list))后,问题得以解决。