题目描述

给出一组候选数C和一个目标数T,找出候选数中加起来和等于T的所有组合。
C中的数字在组合中可以被无限次使用
注意:
  • 题目中所有的数字(包括目标数T)都是正整数
  • 你给出的组合中的数字 (a 1, a 2, … , a k) 要按非递增排序 (ie, a 1 ≤ a 2 ≤ … ≤ a k).
  • 结解集中不能包含重复的组合
 
例如:给定的候选数集是[2,3,6,7],目标数是7
解集是:
[7]
[2, 2, 3]

Given a set of candidate numbers ( C ) 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.
  • Elements in a combination (a 1, a 2, … , a k) must be in non-descending order. (ie, a 1 ≤ a 2 ≤ … ≤ a k).
  • The solution set must not contain duplicate combinations.

For example, given candidate set2,3,6,7and target7, 
A solution set is: 
[7]
[2, 2, 3]

class Solution {
public:
    vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
        vector <vector<int>>res;
        vector<int> temp;
        sort(candidates.begin(),candidates.end());
        Sum(res,temp,candidates,0,target);
        return res;
    }
    void Sum(vector <vector <int>> &res,vector<int> &temp,vector<int >&candidates,int k,int target){
        if (target==0){
            res.push_back(temp);
            return ;
        }
        if (target <0 || k>=candidates.size())
            return ;
        vector<int> t =temp;
        temp.push_back(candidates[k]);
        Sum(res,temp,candidates,k,target-candidates[k]);
        Sum(res,t,candidates,k+1,target);
        
    }
};

leetcode111:combination-sum的更多相关文章

  1. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  2. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. [LeetCode] Combination Sum II 组合之和之二

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  4. [LeetCode] Combination Sum 组合之和

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  5. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. LeetCode:Combination Sum I II

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  7. Combination Sum | & || & ||| & IV

    Combination Sum | Given a set of candidate numbers (C) and a target number (T), find all unique comb ...

  8. 【leetcode】Combination Sum II

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  9. 【leetcode】Combination Sum

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  10. LeetCode Combination Sum III

    原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...

随机推荐

  1. 插头 dp

    插头dp 洛谷 黑题板子? P5056 给出n×m的方格,有些格子不能铺线,其它格子必须铺,形成一个闭合回路.问有多少种铺法? 1.轮廓线 简单地说,轮廓线就是已决策格子和未决策格子的分界线: 2,插 ...

  2. 《Java从入门到失业》第五章:继承与多态(5.8-5.10):多态与Object类

    5.8多态 上面我们了解了向上转型,即一个对象变量可以引用本类及子类的对象实例,这种现象称为多态(polymorphism).多态究竟有什么用呢?我们先学习一个知识点. 5.8.1方法重写 前面我们学 ...

  3. C++ 构造函数、拷贝构造函数、赋值运算符

    <C++ Primer Plus> 12.1 动态内存和类 12.1.1 复习示例和静态类成员 不能在类声明中初始化静态成员变量,这是因为声明描述了如何分配内存,但并不分配内存 如果在头文 ...

  4. 源生代码和H5的交互 android:

    1: 默认的事情: Android 通过内置的UI控件WebView来加载网页.         网页是用一个网络地址来表示的:         其整个使用方法很简单如下:(android不关心实际的 ...

  5. 【C语言编程入门】Do you know 函数?不知道了吧,我来告诉你!

    ☆ 函数 在前面我们已经讲过了一些简单的函数,如程序的主函数main().标准输出函数printf().在C语言中,大多数功能都是依靠函数来实现的.But,你知道什么是函数吗?我猜你肯定不知道. 那么 ...

  6. spring注解@Transactional 和乐观锁,悲观锁并发生成有序编号问题

      需求:系统中有一个自增的合同编号,在满足并发情况下,生成的合同编号是自增的. 测试工具:Apache Jmeter 实现方法: 创建一个数据库表.编号最大值记录表 表结构类似 CREATE TAB ...

  7. 【Azure微服务 Service Fabric 】在SF节点中开启Performance Monitor及设置抓取进程的方式

    前提条件 当我们观察到SF中某一个节点出现CPU不正常的情况,但是由于不能肉眼长期观察,所以可以通过开启Performance Monitor的方式来获取每一个进程的%Processer Time的方 ...

  8. Jmeter入门(4)- 注意事项

    一.中文乱码问题的解决方法 1. 将HTTP请求的内容编码改成UTF-8 2. 修改配置文件jmeter.properties 将jmeter安装目录的bin目录下的jmeter.properties ...

  9. MVC单文件上传

    前言 现在来写下最基础的单文件上传,完成后可以扩展成各种不同的上传方式 HTML <input id="Input_File" type="file" / ...

  10. MVC注册

    前言 最近没什么写的,写个MVC注册巩固一下 HTML @{ Layout = null; } <!DOCTYPE html> <html> <head> < ...