题目描述

给出一组候选数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. 【原创】经验分享:一个小小emoji尽然牵扯出来这么多东西?

    前言 之前也分享过很多工作中踩坑的经验: 一个线上问题的思考:Eureka注册中心集群如何实现客户端请求负载及故障转移? [原创]经验分享:一个Content-Length引发的血案(almost.. ...

  2. 教你怎么在thinkphp 5.1下查看版本号

    在thinkphp 5.1下查看版本号,可直接命令行下面 php think version,就可以查看到tp具体的版本号了.

  3. Oracle 正确删除归档日志的方法

    我们都知道在controlfile中记录着每一个archivelog文件的相关信息,当然们在OS下把这些物理文件delete掉后,在我们的controlfile中仍然记录着这些archivelog文件 ...

  4. 2014年 实验四 B2B模拟实验(二)

    [实验目的] ⑴.熟悉电子合同签订过程 ⑵.掌握网上招标的流程并体会招标对采购商带来的好处 [实验条件] ⑴.个人计算机一台 ⑵.计算机通过局域网形式接入互联网 ⑶.电子商务模拟实验室软件包. [知识 ...

  5. gorm学习地址

    1 gorm curd指南 2 gorm入门指南

  6. elk-安装 通过docker

      一. github地址   https://github.com/deviantony/docker-elk   cd /usr/local/src   git clone https://git ...

  7. linux(centos8):基于java13安装rocketmq-4.7.1(解决jdk不兼容的报错)

    一,Rocketmq是什么? 1, RocketMQ是一个队列模型的消息中间件,具有高性能.高可靠.高实时.分布式特点 相比kafka,rocketmq的实时性更强 2,官方网站: http://ro ...

  8. selenium自动登陆

    import osfrom selenium import webdriverimport time,jsonclass Cookie(object): def __init__(self,drive ...

  9. centos6.8 Mysql5.6.22 升级 mysql-5.7.20

    一.检查系统环境 二.备份数据库 mysqldump –all-databases > allbackupfile.sql (建议:有条件的话可使用图形化界面备份,操作灵活) 三.下载安装文件 ...

  10. Ubuntu 18.04 LTS IP 地址设置

    和之前的版本不太一样, Ubuntu 18.04 的 ip地址设置是用netplan管理的     配置文件在: /etc/netplan/50-cloud-init.yaml 示例文件如下: # T ...