Combination Sum II

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 (a1a2, … , 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]

 
要考虑去重
 
每次都从当前位置选取元素,遇到重复的直接跳过就可以了
 
 
 class Solution {

 public:

     vector<vector<int> > combinationSum2(vector<int> &candidates, int target)

     {

         sort(candidates.begin(),candidates.end());

         vector<vector<int> > result;

         vector<int> tmp;

         backtracking(result,candidates,target,tmp,,);

         return result;

     }

     void backtracking(vector<vector<int> > &result,vector<int> &candidates, int &target,vector<int> tmp, int sum,int index)

     {

         if(sum==target)

         {

             result.push_back(tmp);

             return;

         }

         else

         {

             int sum0=sum;

             for(int i=index;i<candidates.size();i++)

             {

                 if(i>index&&candidates[i]==candidates[i-])

                 {

                     continue;

                 }

                 tmp.push_back(candidates[i]);

                 sum=sum0+candidates[i];

                 if(sum>target)

                 {

                     break;

                 }
backtracking(result,candidates,target,tmp,sum,i+); tmp.pop_back(); } } }
    
 

【leetcode】Combination Sum II的更多相关文章

  1. 【leetcode】Combination Sum II (middle) ☆

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

  2. 【LeetCode】Combination Sum II(组合总和 II)

    这道题是LeetCode里的第40道题. 题目要求: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. can ...

  3. 【Leetcode】【Medium】Combination Sum II

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

  4. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

  5. 【leetcode】Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  6. 【leetcode】Combination Sum

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  7. 【leetcode】Combination Sum III(middle)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  8. 【leetcode】Combination Sum (middle)

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

  9. 【LeetCode】Path Sum II 二叉树递归

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

随机推荐

  1. 使用quartz 定时任务

    Quartz 是一个开源的作业调度框架,它完全由 Java 写成,并设计用于 J2SE 和 J2EE 应用中.它提供了巨大的灵活性而不牺牲简单性.你能够用它来为执行一个作业而创建简单的或复杂的调度. ...

  2. SQL Network Interfaces, error: 50 - 发生了 Local Database Runtime 错误。无法创建自动实例。

    今天在用VS2013自带的LocalDB调整数据库时出错,在网上也搜到许多方案,如卸载SQLServer LocalDB的程序.重新创建实例等都没有解决我的问题,也重新修改以及修复Vs,问题依旧存在, ...

  3. MongoDB的安装及配置

    MongoDB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐. Windows (1). 登录Mongodb官网点击下载 (2). 将zi ...

  4. BZOJ-1045 糖果传递 数学+递推

    1045: [HAOI2008] 糖果传递 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2975 Solved: 1327 [Submit][Sta ...

  5. POJ3422 Kaka's Matrix Travels

    描述 On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels wi ...

  6. javascript显示实时时间

    <html> <script language=Javascript> function time(){ //获得显示时间的div t_div = document.getEl ...

  7. IOS基础之 (十五)知识点

    一 SEL 1. 方法的存储位置 每个类的方法地址列表都存储在类对象中. 每个方法都有一个与之对应的SEL类型的对象. 根据一个SEL对象就可以找到方法的地址,进而调用方法. Person.h #im ...

  8. boost(barrier)

    barrier:栅栏的意思,当barrier bar(3),这三个线程会放到栅栏中,等第三个线程执行时一起唤醒,然后返回 barrier barrier类的接口定义如下: class barrier ...

  9. UVa OJ 194 - Triangle (三角形)

    Time limit: 30.000 seconds限时30.000秒 Problem问题 A triangle is a basic shape of planar geometry. It con ...

  10. mysql 索引 详解

    索引是快速搜索的关键.MySQL索引的建立对于MySQL的高效运行是很重要的.下面介绍几种常见的MySQL索引类型. 在数据库表中,对字段建立索引可以大大提高查询速度.假如我们创建了一个 mytabl ...