can someone plz tell me why this code generate empty "res" variable? If uncomment the commented line and remove below will be working fine.
Codes not work:
class Solution(object):
def dfs(self, nums, res, line):
if not nums:
print(line)
res.append(line)
return
for i, num in enumerate(nums):
line.append(num)
# self.dfs(nums[:i]+nums[i+1:], res, line+[num])
self.dfs(nums[:i]+nums[i+1:], res, line)
line.pop()
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = []
self.dfs(nums, res, [])
print(res)
return res
if __name__ == "__main__":
Solution().permute([1,2])
Works fine if changing to:
for i, num in enumerate(nums):
self.dfs(nums[:i]+nums[i+1:], res, line+[num])
except using append/pop for passing the DFS. Even the "line" variable before appending to "res" is correct.
Does it have something to do with referencing? The only thing I could think of is whatever passed to res got cleaned up. I would really appreciate if someone could show me the link to reference.