题目描述

给出一组候选数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. vscode编写python,引用本地py文件出现红色波浪线

    前言 引用本地py文件出现红色波浪线,如下图: 原因 经过查询得知,vscode中的python插件默认使用的是pylint来做代码检查,因此需要对pylint做一些配置 解决方案 在setting. ...

  2. 阿里百秀后台管理项目笔记 ---- Day01

    摘要 在此记录一下阿里百秀项目的教学视频的学习笔记,部分页面被我修改了,某些页面效果会不一样,基本操作是一致的,好记性不如烂笔头,加油叭!!! step 1 : 整合全部静态页面 将静态页面全部拷贝到 ...

  3. 晋城6397.7539(薇)xiaojie:晋城哪里有xiaomei

    晋城哪里有小姐服务大保健[微信:6397.7539倩儿小妹[晋城叫小姐服务√o服务微信:6397.7539倩儿小妹[晋城叫小姐服务][十微信:6397.7539倩儿小妹][晋城叫小姐包夜服务][十微信 ...

  4. 转 Swoole】用swoole简单实现MySQL连接池

    MySQL连接池 在传统的网站开发中,比如LNMP模式,由Nginx的master进程接收请求然后分给多个worker进程,每个worker进程再链接php-fpm的master进程,php-fpm再 ...

  5. go 爬取页面保存

    package main import ( "bufio" "fmt" "io/ioutil" "net/http" & ...

  6. Navigator 的使用方法

    对象属性 属性 Navigator 说明 appCodeName 返回浏览器的代码名 appName 返回浏览器的名称 appVersion 返回浏览器的平台和版本信息 cookieEnabled 返 ...

  7. Linux运维学习第五周记

    休惊岁岁年年貌 且对朝朝暮暮人 细雨晴时一百六 画船鼍鼓莫违民 雨生百谷,春雨贵如油 第五周学记 这周主要学习了九三级磁盘.存储相关知识和Linux文件系统以及计算机网络的内容 磁盘和文件系统 磁盘结 ...

  8. WebSocket的理解

    Websocket相对于无状态的HTTp协议,是在一次成功连接之后,在关闭请求之前,服务器和客户端能顺利进行信息传输.而不用像HTTP那样每一次都要告诉服务器这个请求者是谁(身份鉴别信息). 在HTT ...

  9. docker 启动容器restart 策略

    docker 运行容器时使用--restart 参数可以指定一个restart策略,来指定容器应该如何重启,或不应该重启,当容器启用restart策略时,将会载docker ps 显示up 或者res ...

  10. java基础第一章

    有一定的基础,但是还是要重新开始,2020.10.6 1.手写Hello World public class HelloWorld{ public static void main(String[] ...