我想知道列表变量本身和后跟[:]
的列表变量之间的区别例如
# When nums are List[int] and res are List,# what is the difference between
res.append(nums[:])
# and
res.append(nums)
我正在实现递归置换功能时出现了我的问题
class Solution:
def permute(self,nums: List[int]) -> List[List[int]]:
res = []
self.helper(nums,len(nums) - 1,res)
return res
def helper(self,nums,l,r,res):
if l == r:
res.append(nums[:]) # this will append unique combination
res.append(nums) # this will result in a list full of same combinations
else:
for i in range(l,r + 1):
nums[l],nums[i] = nums[i],nums[l]
self.helper(nums,l + 1,res)
nums[l],nums[l]
谢谢您的帮助!