| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1458 人关注过本帖
标题:递归中return的作用
只看楼主 加入收藏
zhangyu68
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2019-2-28
收藏
 问题点数:0 回复次数:1 
递归中return的作用
这是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怎么起到了去重的作用
        
        
搜索更多相关主题的帖子: 递归 return target res answer 
2019-02-28 23:00
俺是你大爷
Rank: 2
等 级:论坛游民
帖 子:57
专家分:35
注 册:2019-3-12
收藏
得分:0 
return的作用:返回函数调用前状态。此时栈是空的。
2019-03-12 11:42
快速回复:递归中return的作用
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015115 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved