首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >数据结构算法操作试题(C++/Python)—— 组合总和

数据结构算法操作试题(C++/Python)—— 组合总和

作者头像
莫斯
发布2020-09-09 21:20:36
4460
发布2020-09-09 21:20:36
举报
文章被收录于专栏:备份备份

数据结构算法操作试题(C++/Python):数据结构算法操作试题(C++/Python)——目录


1. 题目

leetcode 链接:https://leetcode-cn.com/problems/combination-sum/

在这里插入图片描述
在这里插入图片描述

2. 解答

python: 58ms, 10.8 mb

class Solution(object):
    def combinationSum(self, candidates, target):
        """
        :type candidates: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        res = []
        candidates.sort()
        def dfs(remain,path):
            if not remain:
                res.append(path)
                return
            for i in candidates:
                if i>remain:
                    break
                elif path and i<path[-1]:
                    continue
                else:
                    dfs(remain-i,path+[i])
        dfs(target,[])
        return res

其他方法看 leetcode 链接 评论区~

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-04-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 题目
  • 2. 解答
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档