美文网首页
组合相加

组合相加

作者: 水瓶鱼 | 来源:发表于2017-03-26 02:01 被阅读5次

题目

Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

The same repeated number may be chosen from C unlimited number of times.

Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7] and target 7,

python

class Solution(object):
    def combinationSum(self, candidates, target):
        """
        :type candidates: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        result=[]
        candidates.sort();
        self.dfs(candidates,target,0,[],result)
        return result
        
    def dfs(self, nums, target, depth, path, res):
        if target < 0:
            return;
        if target == 0:
            res.append(path)
            return
        for i in range(depth, len(nums)):
            self.dfs(nums, target - nums[i], i, path + [nums[i]], res)

解题思路

深度优先遍历

相关文章

  • 组合相加

    题目 Given a set of candidate numbers (C) (without duplicat...

  • 2018-02-23

    047期,关注两码相加等于0的组合

  • leetCode进阶算法题+解析(二十九)

    组合总和3 题目:找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合...

  • 66.集合拆分

    1.用法如下: 2.集合重新组合 3.集合相加

  • 递归与回溯:216.组合总和III

    /** 题目 找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不...

  • 216 组合总和 III

    题目: 找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重...

  • Leetcode 精选之搜索( 组合总和 III)

    题目描述 找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在...

  • 216. 组合总和 III

    找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字...

  • 216. 组合总和 III

    找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字...

  • 216. Combination Sum III

    找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字...

网友评论

      本文标题:组合相加

      本文链接:https://www.haomeiwen.com/subject/phphottx.html