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. jQuery插件开发模式

    jQuery插件开发模式 软件开发过程中是需要一定的设计模式来指导开发的,有了模式,我们就能更好地组织我们的代码,并且从这些前人总结出来的模式中学到很多好的实践. 根据<jQuery高级编程&g ...

  2. 【 Jquery插件】引导用户如何操作网站功能的向导

    Joyride是一个jQuery插件,可以利用它来创建一个引导用户如何操作网站功能的向导.通过定义一个操作步骤顺序,这个插件会在需要操作的HTML元素旁边显示一个帮助说明的Tooltips. http ...

  3. Myeclipse快捷键的使用

    存盘 Ctrl+s(肯定知道) 注释代码 Ctrl+/ 取消注释 Ctrl+\(Eclipse3已经都合并到Ctrl+/了) 代码辅助 Alt+/ 快速修复 Ctrl+1 代码格式化 Ctrl+Shi ...

  4. BZOJ-1008 越狱 数论快速幂

    1008: [HNOI2008]越狱 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 6192 Solved: 2636 [Submit][Status] ...

  5. 最短路之Dijkstra算法

    1. 邻接矩阵 int cost[MAX_V][MAX_V]; //assume cost[u][v]>0 int d[MAX_V]; bool used[MAX_V]; void Dijkst ...

  6. git分支与版本管理、版本回退、冲突解决记录

    一.基础使用 1.初始化本地仓库 git init 2.关联远程仓库 git remote add origin git@github.com:用户名/仓库名.git 3.添加远程仓库文件到本地 gi ...

  7. 蓝桥T291(BFS + 输出路径)

    http://lx.lanqiao.org/problem.page?gpid=T291 学霸的迷宫   时间限制:1.0s   内存限制:256.0MB      问题描述 学霸抢走了大家的作业,班 ...

  8. MyEclipse------遍历某个路径下的(所有或特定)文件和目录

    usebean包(自己定义的,在src文件夹下面)里的java文件 FileAccept.java package usebean; import java.io.File; import java. ...

  9. js ajax 向后台传递数组

    //定义数组 var _arrays = []; for (var i = 0; i < 10; i++) { _arrays.push(i); } //post $.post(href, {l ...

  10. Android-调用优酷SDK上传视频

    最近在研究用优酷的SDK来进行视频上传的功能,由于优酷的SDK只是提供了一个上传的sample code,没有涉及到授权的过程,对于新手来说,可能非常棘手,现在分享一下我的思路:   程序实现前我们先 ...