一天一道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. android自定义View之3D索引效果

    效果图: 我的小霸王太卡了. 最近工作比较忙,今天搞了一下午才搞出来这个效果,这种效果有很多种实现方式,最常见的应该是用贝塞尔曲线实现的.今天我们来看另一种不同的实现方式,只需要用到 canvas.s ...

  2. 在做自动化测试之前你需要知道的,转自:http://www.cnblogs.com/fnng/p/3653793.html

    什么是自动化测? 做测试好几年了,真正学习和实践自动化测试一年,自我感觉这一个年中收获许多.一直想动笔写一篇文章分享自动化测试实践中的一些经验.终于决定花点时间来做这件事儿. 首先理清自动化测试的概念 ...

  3. CentOS6.7 下安装JDK

    第一步:从官网上下载rpm版本的jdk文件. 第二步:安装JDK 执行命令rpm    -ivh    jdk-8u73-linux-i586.rpm 至此,JDK安装完成,我们来看下JDK的安装目录 ...

  4. 如何通过网络连接进行ADB调试

    点击打开链接 大家在使用adb调试Android系统时可能会遇到麻烦,比如usb端口只有一个,如果用作adb调试,就不能通过usb连接其它器件,或者usb端口不能使用时也没法进行adb调试. Andr ...

  5. 详解EBS接口开发之采购申请导入

    更多内容可以参考我的博客  详解EBS接口开发之采购订单导入 http://blog.csdn.net/cai_xingyun/article/details/17114697 /*+++++++ ...

  6. Transform介绍(Unity3D开发之二)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=491 可能unity中接触较早的 ...

  7. Handler,MessageQueue Loop 和Message的原理解析

    先介绍和handler一起工作的几个组件 Handler的方法介绍 代码示例 package liu.peng.weather; import java.util.Timer; import java ...

  8. 秒懂ASP.NET中的内置对象

    上篇博客,小编主要简单的介绍了一下ASP.NET中的控件,这篇博客,小编主要简单总结一下ASP.NET中的内置对象,七个内置对象分别是:Request.Response.Application.Coo ...

  9. findbugs, checkstyle, pmd的myeclipse7.5+插件安装(转:http://blog.csdn.net/priestmoon/article/details/63941)

    CheckStyle (1)下载net.sf.eclipsecs_5.3.0.201012121300-updatesite-.zip (2)打开MyEclipse,Help->Software ...

  10. Android5.1设备无法识别exFAT文件系统的64G TF卡问题

    64G TF卡刚买回来的时候默认exFAT文件系统,在电脑端(XP和WIN7)可以识别,但在我们Android5.1S设备无法识别,采用guiformat工具格式化为FAT32文件系统后才可以正常识别 ...