注册 登录
编程论坛 Python论坛

递归中return的作用

zhangyu68 发布于 2019-02-28 23:00, 1573 次点击
这是leetcode 组合总和的题,我使用递归写完之后,发现有重复的,python的set()无法直接对二维表进行set,有人直接加了一条return就达到了目的,不太理解,请问一下

class Solution(object):
    def combinationSum(self, candidates, target):
        """
        :type candidates: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        candidates.sort()
        res = []
        ans = []
        (target,candidates,0,res,ans)
        print(res)
        return res
   
    def compute(self,target,candidates,start,result,answer):
        for i in range(start,len(candidates)):
            if target > 0:
                answer.append(candidates[i])
                (target-candidates[i],candidates,i,result,answer)
                answer.pop()
               
            if target < 0:
                pass
            
            if target == 0:
                tmp = answer[:]
                result.append(tmp)
                return
               
       请问这里return怎么起到了去重的作用
        
        
1 回复
#2
俺是你大爷2019-03-12 11:42
return的作用:返回函数调用前状态。此时栈是空的。
1