一天一道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. [CSDN_Markdown] 使用CSDN Markdown编辑器

    简介 最近CSDN支持Markdown语法写博客了,甚是欢喜.前几天写了一篇实验了下,感觉不错.准备写几篇文章介绍一下如何使用CSDN的Markdown编辑器写博客,不求全面,但求够用,望大家批评指正 ...

  2. Gradle 1.12用户指南翻译——第五十一章. 发布构件

    本文由CSDN博客貌似掉线翻译,其他章节的翻译请参见:http://blog.csdn.net/column/details/gradle-translation.html翻译项目请关注Github上 ...

  3. Android Multimedia框架总结(十七)音频开发基础知识

    请尊重分享成果,转载请注明出处,本文来自逆流的鱼yuiop,原文链接:http://blog.csdn.net/hejjunlin/article/details/53078828 近年来,唱吧,全民 ...

  4. UE4使用第三方库读写xml文件

    原文链接:http://gad.qq.com/article/detail/7181031 本文首发腾讯GAD开发者平台,未经允许,不得转载 在游戏开发过程中,读写xml几乎已经成为不可或缺的功能,但 ...

  5. 打开CMDLINE中的 ” earlyprink “ 参数

    点击打开链接 解决问题的过程中,好文章推荐,都保存在火狐wilson_sq@qq.com记录中~~~~~~~~grep -r "earlyprintk" kernelkernel/ ...

  6. EBS业务学习之采购管理

    一.基础数据 w供应商档案 w采购员设置 w审批层次 w单据控制 w危险类代码 w检验代码 w自动来源 w供应商项目w目录册 二.业务流程 w请购单 w询价单 w报价单 w采购定单 w接收 w检验 w ...

  7. Html书写规范,基本标签使用

    一.html简介1.html是什么Html是用来描述网页的一种语言.(1)HTML 指的是超文本标记语言 (Hyper Text Markup Language)(2)HTML 不是一种编程语言,而是 ...

  8. 1CCTableView的使用,TableView响应和小格子tableView实现

    1        CCTableView的使用 T26TableView.h #ifndef __T26TableView_H__ #define __T26TableView_H__ #includ ...

  9. 3-sum问题

    给定一个整数数组,判断能否从中找出3个数a.b.c,使得他们的和为0,如果能,请找出所有满足和为0个3个数对. #define SIZE 10 void judgeAndPut(int* arr, i ...

  10. Gradle 的Daemon配置

    最近升级到Android 2.2.2之后,运行之前的项目特别卡,基本上2分钟,好的时候1分半,查询了Android官网的说明说daemon能够加快编译.于是我也尝试开启Daemon. 在Windows ...