温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

leetcode_ 组合和

发布时间:2020-02-22 06:34:02 来源:网络 阅读:181 作者:专注地一哥 栏目:web开发

typing import List
class Solution:
def combinationSum1(self, candidates: List[int], target: int) -> List[List[int]]:
#必须要排序 最后的结果可能会有顺序不一样但是元素一样的组合
candidates.sort()
res = []
reslist = []

    def dfs(candidates,target,reslist):
        if target == 0 and reslist not in res:
            res.append(reslist)
            return
        if target<0:
            return

        for i in range(len(candidates)):
            print(reslist)
            #这个递归太难,再理解
            dfs(candidates[i+1:],target-candidates[i],reslist+[candidates[i]])

    dfs(candidates,target,reslist)
            function(){ //外汇点差  http://www.kaifx.cn/mt4/kaifx/1749.html
    return res

def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
    candidates.sort()
    # for i in range(candidates):
    res = []
    reslist = []

    def dfs(candidates,begin,target,reslist):
        # for i in range(len(candidates)):
        if target == 0 and reslist not in res:
            res.append(reslist)
            return
        elif target<0 or begin>=len(candidates):
            return
        else:
            # print(reslist,begin)
            dfs(candidates,begin+1,target,reslist)
            dfs(candidates,begin+1,target-candidates[begin],reslist+[candidates[begin]])

    dfs(candidates,0,target,reslist)
    return res

if name == "main":

a = Solution()
print(a.combinationSum1([2,3,6,7],7))
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI