一天一道LeetCode系列

(一)题目

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

Each number in C may only be used once in the combination.

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 10,1,2,7,6,1,5 and target 8,

A solution set is:

[1, 7]

[1, 2, 5]

[2, 6]

[1, 1, 6]

(二)解题

具体思路与【一天一道LeetCode】39. Combination Sum这篇博文一样,采用动态规划和回溯法进行求解。

/*
和上一题的思路一样,区别是数字不能重复查找,但Vector中允许有重复的数字
具体改动请看代码注释
*/
class Solution {
public:
    vector<vector<int>> ret;
    vector<vector<int>> combinationSum2(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;
    }
    void combinationDfs(vector<int>& candidates ,vector<int>& tmp, int start ,int target)
    {
        if(target == 0){
            ret.push_back(tmp);
            return;
        }
        for(int i = start+1 ; i < candidates.size() ; i++)//从start+1开始查找,避免了数字重复查找
        {
            if(candidates[i] < target){
                tmp.push_back(candidates[i]);
                combinationDfs(candidates,tmp,i,target-candidates[i]);
                tmp.pop_back(); //回溯
            }
            else if(candidates[i] == target){
                tmp.push_back(candidates[i]);
                ret.push_back(tmp);
                tmp.pop_back();//回溯
            }
            else
            {
                return;
            }
            while(i+1< candidates.size()&&candidates[i]==candidates[i+1]) i++;//去除重复的查找
        }
    }
};

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

  1. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  2. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  3. [LeetCode] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  4. [LeetCode] 40. Combination Sum II 组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  5. LeetCode 40. Combination Sum II (组合的和之二)

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

  6. Leetcode 40. Combination Sum II

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

  7. leetcode 40 Combination Sum II --- java

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

  8. [LeetCode] 40. Combination Sum II ☆☆☆(数组相加等于指定的数)

    https://leetcode.wang/leetCode-40-Combination-Sum-II.html 描述 Given a collection of candidate numbers ...

  9. Java [Leetcode 40]Combination Sum II

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

  10. LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)

    题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description   给定数组,数组中的元素均为正数,target也是正数. ...

随机推荐

  1. 项目分享:通过使用SSH框架的公司-学员关系管理系统(CRM)

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

  2. hadoop入门级总结一:HDFS

    虽然hadoop经历了多年的发展,作为技术人员都或多或少的使用过或者了解过.这里还是做一个简单的总结,主要原因是之前主要是做hadoop的开发,对hadoop的运维知之甚少,但真正的接触到hadoop ...

  3. python用openpyxl操作excel

    python操作excel方法 1)自身有Win32 COM操作office但讲不清楚,可能不支持夸平台,linux是否能用不清楚,其他有专业处理模块,如下 2)xlrd:(读excel)表,xlrd ...

  4. Kafka系列之-Kafka监控工具KafkaOffsetMonitor配置及使用

    KafkaOffsetMonitor是一个可以用于监控Kafka的Topic及Consumer消费状况的工具,其配置和使用特别的方便.源项目Github地址为:https://github.com/q ...

  5. Swift 3中新的访问控制关键字fileprivate和open

    在Swift 3中除去原有的3个访问控制关键字private,public,internal,又添加了2个关键字fileprivate和open 它们可以看成是对private和public的进一步细 ...

  6. 27 自定义View小结

    自定义View 1 为了满足开发需要 就需要自定义View 2 分类: 直接继承View 继承View的子类(现有控件 button,TextView-.) 继承ViewGroup(线性布局 相对布局 ...

  7. Android中GridView的一些特殊属性

    GridView的一些特殊属性: 1.android:numColumns="auto_fit"   //GridView的列数设置为自动 2.android:columnWidt ...

  8. Struts 1 之配置文件

    web.xml中配置Struts的入口Servlet--ActionServlet,ActionServlet不负责任何的业务处理,它只是查找Action名单,找到path属性与URL属性一致的Act ...

  9. linux C++多线程操作文件&输出加锁

    下述demo将指定目录下文件存入vector,然后一分为二交给两个线程(threadTask1,threadTask2)去分别处理,对输出函数printDirent加锁防止紊乱. #include & ...

  10. Android摄像头照相机技术-android学习之旅(八)

    简介 Android SDK支持Android设备内置的照相机.从Android2.3开始支持多个摄像头(主要指前置摄像头和后置摄像头).通过照片相可以拍照和录像. 需要考虑的问题 是否支持照相机 快 ...