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

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

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) -&gt; List[List[int]]:
        output =[]
        i =0
        def rec(i,target,output,candidates):
            if i&gt;=len(candidates):
                if target == 0: 
                    self.slack.append(output) 
                return self.slack
            
            if candidates[i]&lt;=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) -&gt; List[List[int]]:
        output = []
        i = 0
        candidates.sort()  # Sort the candidates list to handle duplicates

        def rec(i, target, output, candidates):
            if i &gt;= len(candidates):
                if target == 0:
                    self.slack.append(output.copy())  # Append a copy of &#39;output&#39; to &#39;self.slack&#39;
                return self.slack

            if candidates[i] &lt;= target:
                output.append(candidates[i])
                rec(i + 1, target - candidates[i], output, candidates)

                # Skip duplicates
                while i + 1 &lt; 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]]

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:

确定