为什么在Python递归中我的列表中得到了None?

huangapple go评论123阅读模式
英文:

why am i getting None in my list in Recursion in python

问题

我的 Slack 输出为 slack=[[],[],[]],为什么输出没有追加进去呢?

  1. class Solution:
  2. def __init__(self):
  3. self.slack = []
  4. def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
  5. output = []
  6. i = 0
  7. def rec(i, target, output, candidates):
  8. if i >= len(candidates):
  9. if target == 0:
  10. self.slack.append(output[:]) # 需要使用副本,而不是直接追加
  11. return self.slack
  12. if candidates[i] <= target:
  13. output.append(candidates[i])
  14. rec(i + 1, target - candidates[i], output, candidates)
  15. output.pop()
  16. rec(i + 1, target, output, candidates)
  17. rec(i, target, output, candidates)
  18. return self.slack

需要在self.slack.append(output[:])中使用副本来追加输出,而不是直接追加 output,以确保正确追加结果。

英文:

my slack is getting o/p as slack=[[],[],[]] output is not appending in it why?
candidates = [10,1,2,7,6,1,5], target = 8 Output: [ [1,1,6], [1,2,5], [1,7], [2,6] ]

  1. type here
  2. class Solution:
  3. def __init__(self):
  4. self.slack =[]
  5. def combinationSum2(self, candidates: List[int], target: int) -&gt; List[List[int]]:
  6. output =[]
  7. i =0
  8. def rec(i,target,output,candidates):
  9. if i&gt;=len(candidates):
  10. if target == 0:
  11. self.slack.append(output)
  12. return self.slack
  13. if candidates[i]&lt;=target:
  14. output.append(candidates[i])
  15. rec(i+1,target-candidates[i],output,candidates)
  16. output.pop()
  17. rec(i+1,target,output,candidates)
  18. rec(i,target,output,candidates)
  19. return self.slack

output needs to be appended inside the slack

答案1

得分: 1

  1. from typing import List
  2. class Solution:
  3. def __init__(self):
  4. self.slack = []
  5. def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
  6. output = []
  7. i = 0
  8. candidates.sort() # 对候选项进行排序以处理重复项
  9. def rec(i, target, output, candidates):
  10. if i >= len(candidates):
  11. if target == 0:
  12. self.slack.append(output.copy()) # 将 'output' 的副本附加到 'self.slack'
  13. return self.slack
  14. if candidates[i] <= target:
  15. output.append(candidates[i])
  16. rec(i + 1, target - candidates[i], output, candidates)
  17. # 跳过重复项
  18. while i + 1 < len(candidates) and candidates[i] == candidates[i + 1]:
  19. i += 1
  20. output.pop() # 回溯时移除最后一个元素
  21. rec(i + 1, target, output, candidates)
  22. rec(i, target, output, candidates)
  23. return self.slack
  24. # 测试
  25. candidates = [10, 1, 2, 7, 6, 1, 5]
  26. target = 8
  27. solution = Solution()
  28. result = solution.combinationSum2(candidates, target)
  29. print(result) # [[1, 1, 6], [1, 2, 5], [1, 7], [2, 6]]
  30. assert result == [[1, 1, 6], [1, 2, 5], [1, 7], [2, 6]]
英文:

Try this

  1. from typing import List
  2. class Solution:
  3. def __init__(self):
  4. self.slack = []
  5. def combinationSum2(self, candidates: List[int], target: int) -&gt; List[List[int]]:
  6. output = []
  7. i = 0
  8. candidates.sort() # Sort the candidates list to handle duplicates
  9. def rec(i, target, output, candidates):
  10. if i &gt;= len(candidates):
  11. if target == 0:
  12. self.slack.append(output.copy()) # Append a copy of &#39;output&#39; to &#39;self.slack&#39;
  13. return self.slack
  14. if candidates[i] &lt;= target:
  15. output.append(candidates[i])
  16. rec(i + 1, target - candidates[i], output, candidates)
  17. # Skip duplicates
  18. while i + 1 &lt; len(candidates) and candidates[i] == candidates[i + 1]:
  19. i += 1
  20. output.pop() # Remove the last element as we backtrack
  21. rec(i + 1, target, output, candidates)
  22. rec(i, target, output, candidates)
  23. return self.slack

Testing

  1. candidates = [10, 1, 2, 7, 6, 1, 5]
  2. target = 8
  3. solution = Solution()
  4. result = solution.combinationSum2(candidates, target)
  5. print(result) # [[1, 1, 6], [1, 2, 5], [1, 7], [2, 6]]
  6. assert result == [[1,1,6], [1,2,5], [1,7], [2,6]]

huangapple
  • 本文由 发表于 2023年7月23日 16:32:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定