题目描述

给出一组候选数C和一个目标数T,找出候选数中起来和等于T的所有组合。
C中的每个数字在一个组合中只能使用一次。
注意:
  • 题目中所有的数字(包括目标数T)都是正整数
  • 组合中的数字 (a 1, a 2, … , a k) 要按非递增排序 (ie, a 1 ≤ a 2 ≤ … ≤ a k).
  • 结果中不能包含重复的组合
 
例如:给定的候选数集是[10,1,2,7,6,1,5],目标数是8
解集是:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]

Given a collection of candidate numbers ( C ) and a target number
( T ), find all unique combinations in Cwhere 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 (a 1, a 2, … , a k) must be in non-descending order. (ie, a 1 ≤ a 2 ≤ … ≤ a k).
  • The solution set must not contain duplicate combinations.

For example, given candidate set10,1,2,7,6,1,5and target8, 
A solution set is: 
[1, 7]
[1, 2, 5]
[2, 6]

class Solution {
    vector< int> d;
    vector <vector <int>> z;
    set<vector<int>> s;
    
public:
    vector<vector<int> > combinationSum2(vector<int> &num, int target) {
        sort(num.begin(),num.end());
        dfs(num,0,target,0);
        set<vector<int>> ::iterator it;
        for (it=s.begin();it!=s.end();z.push_back(*it),it++);
        return z;
        
    }
    void dfs(vector<int> & candidates,int x ,int t, int step){
        if (x>t || x<0 )  return ;
        if (x==t){s.insert(d);}
        else
        {
            int i;
            for (i=step;i<candidates.size();i++){
                d.push_back(candidates[i]);
                dfs(candidates,x+candidates[i],t,i+1);
                d.pop_back();
            }
        }
    }
};

leetcode110:combination-sum-ii的更多相关文章

  1. 【leetcode】Combination Sum II

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  2. Combination Sum,Combination Sum II,Combination Sum III

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

  3. [Leetcode][Python]40: Combination Sum II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...

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

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

  5. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  6. 【LeetCode】40. Combination Sum II (2 solutions)

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  7. LeetCode: Combination Sum II 解题报告

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  8. LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

  9. leetcode-combination sum and combination sum II

    Combination sum: Given a set of candidate numbers (C) and a target number (T), find all unique combi ...

  10. Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)

    Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

随机推荐

  1. JVM 内存分配和占用

    我们从一个简单示例来引出JVM的内存模型 简单示例 我从一个简单示例谈起这一块,我在看一篇文章的时候看到这么一个场景并且自己做了尝试,就是分配一个2M的数组,使用Xmx即最大内存为12M的话,会报错J ...

  2. Docker笔记5:实现加速器,加快下载/拉取镜像速度

    由于 Docker 官方仓库存储于国外服务器,因此,我们使用d ocker pull 命令拉取镜像时,速度很慢.但是,我们可以使用国内服务商提供的加速器进行加速,加速器实质是一个IP地址,将其加入到d ...

  3. Brew error: Could not symlink, path is not writable

    As explained here by Rick: Start with brew doctor which will show you errors with your brew setup. Y ...

  4. 一道算法题,引出collections.Counter的特殊用法

    题目描述: 题目编号:1002. 查找常用字符 给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表.例如,如果一个字符在每个字符串中出现 3 次, ...

  5. json对象去重,根据指定字段

    function FilterByName(data, Name) { //data是json对象,Name是根据什么字段去重 var map = {}, dest = []; for (var i ...

  6. C语言从1打印到100再打印到1该如何编写?我只服最后一种写法!

    我觉得这是一个送分题,奈何人才太多了,给出了各种古怪的写法,如果是做项目的话,我比骄建议一些正常的写法,就是大家都能看得懂的,不要搞什么花里胡哨,不过你要是交流的话,既然是交流,我不觉得要多正规,即使 ...

  7. Anno 框架 增加缓存、限流策略、事件总线、支持 thrift grpc 作为底层传输

    github 地址:https://github.com/duyanming/dymDemo dym 分布式开发框架 Demo 熔断 限流 事件总线(包括基于内存的.rabbitmq的) CQRS D ...

  8. day69:Vue:组件化开发&Vue-Router&Vue-client

    目录 组件化开发 1.什么是组件? 2.局部组件 3.全局组件 4.父组件向子组件传值 5.子组件往父组件传值 6.平行组件传值 Vue-Router的使用 Vue自动化工具:Vue-Client 组 ...

  9. msyql查看连接数

    连接数 SHOW FULL PROCESSLIST 1.  查看允许的最大并发连接数 SHOW VARIABLES LIKE 'max_connections'; 2.  修改最大连接数 方法1:临时 ...

  10. linux(centos8):jmeter5.3并发测试实例(参数在范围内随机取值)

    一,测试的url地址说明: 1,这是一个秒杀功能的url: http://127.0.0.1:8080/second/skusecond?actid=2020&skuid=cpugreen&a ...