英文:
why am i getting None in my list in Recursion in python
问题
我的 Slack 输出为 slack=[[],[],[]],为什么输出没有追加进去呢?
class Solution:
def __init__(self):
self.slack = []
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
output = []
i = 0
def rec(i, target, output, candidates):
if i >= len(candidates):
if target == 0:
self.slack.append(output[:]) # 需要使用副本,而不是直接追加
return self.slack
if candidates[i] <= target:
output.append(candidates[i])
rec(i + 1, target - candidates[i], output, candidates)
output.pop()
rec(i + 1, target, output, candidates)
rec(i, target, output, candidates)
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] ]
type here
class Solution:
def __init__(self):
self.slack =[]
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
output =[]
i =0
def rec(i,target,output,candidates):
if i>=len(candidates):
if target == 0:
self.slack.append(output)
return self.slack
if candidates[i]<=target:
output.append(candidates[i])
rec(i+1,target-candidates[i],output,candidates)
output.pop()
rec(i+1,target,output,candidates)
rec(i,target,output,candidates)
return self.slack
output needs to be appended inside the slack
答案1
得分: 1
from typing import List
class Solution:
def __init__(self):
self.slack = []
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
output = []
i = 0
candidates.sort() # 对候选项进行排序以处理重复项
def rec(i, target, output, candidates):
if i >= len(candidates):
if target == 0:
self.slack.append(output.copy()) # 将 'output' 的副本附加到 'self.slack'
return self.slack
if candidates[i] <= target:
output.append(candidates[i])
rec(i + 1, target - candidates[i], output, candidates)
# 跳过重复项
while i + 1 < len(candidates) and candidates[i] == candidates[i + 1]:
i += 1
output.pop() # 回溯时移除最后一个元素
rec(i + 1, target, output, candidates)
rec(i, target, output, candidates)
return self.slack
# 测试
candidates = [10, 1, 2, 7, 6, 1, 5]
target = 8
solution = Solution()
result = solution.combinationSum2(candidates, target)
print(result) # [[1, 1, 6], [1, 2, 5], [1, 7], [2, 6]]
assert result == [[1, 1, 6], [1, 2, 5], [1, 7], [2, 6]]
英文:
Try this
from typing import List
class Solution:
def __init__(self):
self.slack = []
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
output = []
i = 0
candidates.sort() # Sort the candidates list to handle duplicates
def rec(i, target, output, candidates):
if i >= len(candidates):
if target == 0:
self.slack.append(output.copy()) # Append a copy of 'output' to 'self.slack'
return self.slack
if candidates[i] <= target:
output.append(candidates[i])
rec(i + 1, target - candidates[i], output, candidates)
# Skip duplicates
while i + 1 < len(candidates) and candidates[i] == candidates[i + 1]:
i += 1
output.pop() # Remove the last element as we backtrack
rec(i + 1, target, output, candidates)
rec(i, target, output, candidates)
return self.slack
Testing
candidates = [10, 1, 2, 7, 6, 1, 5]
target = 8
solution = Solution()
result = solution.combinationSum2(candidates, target)
print(result) # [[1, 1, 6], [1, 2, 5], [1, 7], [2, 6]]
assert result == [[1,1,6], [1,2,5], [1,7], [2,6]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论