一天一道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. 文件一键上传、汉字转拼音、excel文件上传下载功能模块的实现

    ----------------------------------------------------------------------------------------------[版权申明: ...

  2. Sublime Text 3下C/C++开发环境搭建

    Sublime Text 3下C/C++开发环境搭建 之前在Linux Mint 17一周使用体验中简单介绍过Sublime Text. 1.Sublime Text 3安装 Ubuntu.Linux ...

  3. 在MPAndroidChart库K线图的基础上画均线

    CombinedChart 可以直接使用MPAndroidChart库里面提供的CombinedChart实现组合图形 Demo:CombinedChartDemo ------分割线(如果想在一个图 ...

  4. CentOS 7 下使用虚拟环境Virtualenv安装Tensorflow cpu版记录

    1.首先安装pip-install 在使用centos7的软件包管理程序yum安装python-pip的时候会报一下错误: No package python-pip available. Error ...

  5. 【mybatis深度历险系列】mybatis的框架原理+入门程序解析

    在前面的博文中,小编介绍了springmvc的相关知识点,在今天这篇博文中,小编将介绍一下mybatis的框架原理,以及mybatis的入门程序,实现用户的增删改查,她有什么优缺点以及mybatis和 ...

  6. Writing Sentences [1]

    1) try 'there will be' instead of 'then' In homogeneity MRF, if and , there will be even if . 2) in ...

  7. CountDownLatch使用

    分享牛原创,CountDownLatch类的使用,CountDownLatch是一个工具类,运行主线程开启子线程的时候,子线程还没有结束的时候,主线程可以一直等待,直到初始化的现成的计数器count为 ...

  8. OpenCV+python 人脸识别

    首先给大家推荐一本书:机器学习算法原理与编程实践 本文内容全部转载于书中,相当于一个读书笔记了吧 绪论 1992年麻省理工学院通过实验对比了基于结构特征的方法与基于模版匹配的方法,发现模版匹配的方法要 ...

  9. SpringMVC,MyBatis项目中兼容Oracle和MySql的解决方案及其项目环境搭建配置、web项目中的单元测试写法、HttpClient调用post请求等案例

     要搭建的项目的项目结构如下(使用的框架为:Spring.SpingMVC.MyBatis): 2.pom.xml中的配置如下(注意,本工程分为几个小的子工程,另外两个工程最终是jar包): 其中 ...

  10. Python pygame安装过程笔记

    今天看到一个教程,是关于Python安装pygame模块的.觉得很好,拿来分享一下. 安装Python 额,这个小题貌似在这里很是多余啊.但是为了照顾到刚刚学习Python的童鞋,我还是多啰嗦两句吧. ...