一天一道LeetCode系列

(一)题目

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 (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).

The solution set must not contain duplicate combinations.

For example, given candidate set 2,3,6,7 and target 7,

A solution set is:

[7]

[2, 2, 3]

(二)解题


/*

主要思路:

1.对目标vector排序,定义一个临时的vector容器tmp

2.采用动态规划和回溯法的方法,对于当前要添加到tmp的数num

(1)如果target<num,return;

(2)如果target>num,则继续在num和num以后的数中选择一个数相加;(此处需要考虑可以重复的组合)

(3)如果target==num,则代表找到

对于(2),(3)递归完成后需要将压入的数弹出,即采用回溯法的思想

*/

class Solution {

public:

    vector<vector<int>> ret;//结果

    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {

        sort(candidates.begin(),candidates.end());//首先进行排序

        for(int idx = 0;idx<candidates.size();idx++)

        {

           if(idx-1>=0 && candidates[idx] == candidates[idx-1]) continue;//避免重复查找

           else{

               if(candidates[idx]<=target){//如果小于则调用动态规划函数

                  vector<int> tmp;

                  tmp.push_back(candidates[idx]);

                  combinationDfs(candidates,tmp,idx,target-candidates[idx]); 

               }

           }

        }

        return ret;

    }

    /*candidates为目标vector

    tmp为临时vector变量,存储找到的组合

    start是初始序号,代表在start及其以后的数字中查找

    target是当前需要在candidates中查找的数

    */

    void combinationDfs(vector<int>& candidates , vector<int>& tmp , int start,int target)

    {

        if(target == 0){//如果target等于0,代表第一个数就满足

            ret.push_back(tmp);

            return;

        }

        for(int idx = start;idx<candidates.size();idx++)

        {

            if(target > candidates[idx])

            {

                tmp.push_back(candidates[idx]);

                combinationDfs(candidates,tmp,idx,target-candidates[idx]);

                tmp.pop_back();//回溯法,要pop出最后压入的元素

            }

            else if(target == candidates[idx])//等于target代表以及找到

            {

                tmp.push_back(candidates[idx]);

                ret.push_back(tmp);

                tmp.pop_back();//回溯法,要pop出最后压入的元素

            }

            else if(target < candidates[idx])

            {

                return;

            }

        }

    }

};

【一天一道LeetCode】#39. Combination Sum的更多相关文章

  1. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  2. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  3. [LeetCode] 39. Combination Sum 组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  4. LeetCode 39. Combination Sum (组合的和)

    Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...

  5. Java [Leetcode 39]Combination Sum

    题目描述: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in  ...

  6. LeetCode 39 Combination Sum(满足求和等于target的所有组合)

    题目链接: https://leetcode.com/problems/combination-sum/?tab=Description   Problem: 给定数组并且给定一个target,求出所 ...

  7. [LeetCode] 39. Combination Sum ☆☆☆(数组相加等于指定的数)

    https://leetcode.wang/leetCode-39-Combination-Sum.html 描述 Given a set of candidate numbers (candidat ...

  8. Leetcode 39. Combination Sum

    Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...

  9. leetcode 39 Combination Sum --- java

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

  10. [leetcode]39. Combination Sum组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

随机推荐

  1. Linux块设备加密之dm-crypt分析

    相关的分析工作一年前就做完了,一直懒得写下来.现在觉得还是写下来,以来怕自己忘记了,二来可以给大家分享一下自己的研究经验. 这篇文章算是<Device Mapper代码分析>的后续篇,因为 ...

  2. Linux telnet远程登录操作

    telnet  (如果不行 可以却换root帐户试试 su - root) 1.安装telnet-server     sudo dpkg -i xinetd_1%3a2.3.14-7ubuntu3_ ...

  3. [ExtJS5学习笔记]第二十七节 CMD打包错误 Error C2009: YUI Parse Error (identifier is a reserved word => debugger;)

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/41242993 本文作者:sushengmiyan ------------------ ...

  4. Android传感器

    Android传感器 开发传感器应用 1. 获取传感器管理者对象 // 获取传感器管理者对象 SensorManager mSensorManager = (SensorManager) getSys ...

  5. iOS下JS与OC互相调用(五)--UIWebView + WebViewJavascriptBridge

    WebViewJavascriptBridge是一个有点年代的JS与OC交互的库,使用该库的著名应用还挺多的,目前这个库有7000+star.我去翻看了它的第一版本已经是4年前了,在版本V4.1.4以 ...

  6. storm如何部署拓扑

    storm集群搭建 比较简单,参考官方文档即可http://storm.apache.org/releases/1.0.2/Setting-up-a-Storm-cluster.html 启动Nimb ...

  7. Apache Commons Configuration读取xml配置

    近期项目自己手写一个字符串连接池.因为环境不同有开发版本.测试版本.上线版本.每一个版本用到的数据库也是不一样的.所以需要能灵活的切换数据库连接.当然这个用maven就解决了.Apache Commo ...

  8. Ubuntu15.10 安装OpenCV3.1

    wget https://sourceforge.net/projects/opencvlibrary/files/opencv-unix/3.1.0/opencv-3.1.0.zip/downloa ...

  9. scala学习笔记2(类,继承,抽象类)

    class Person{ // _ 是占位符; var name : String = _ val age : Int = 27 // private[this] 定义的内容无法外部使用,起到保护作 ...

  10. Android异常:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original

    Android异常:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that cr ...