题目描述

给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用一次。

说明:

  • 所有数字(包括目标数)都是正整数。
  • 解集不能包含重复的组合。

示例 1:

输入: candidates = [10,1,2,7,6,1,5], target = 8,
所求解集为:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]

示例 2:

输入: candidates = [2,5,2,1,2], target = 5,
所求解集为:
[
  [1,2,2],
  [5]
]

解题思路

利用回溯思想,首先将数组从小到大排序,然后从第一个数开始,若当前数字和小于目标,则从当前索引的数开始向后依次加入到当前vector中,并更新当前索引和数字和,再从下一个索引重复此动作,直到遇到数字和等于目标。

代码

 class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> res;
vector<int> v;
sort(candidates.begin(), candidates.end());
cmb(candidates, target, v, , , res);
return res;
}
void cmb(vector<int> candidates, int target, vector<int> &v, int idx, int sum, vector<vector<int>> &res){
if(sum == target)
res.push_back(v);
else if(sum < target){
for(int i = idx; i < candidates.size(); i++){
if(i > idx && candidates[i] == candidates[i - ])
continue;
v.push_back(candidates[i]);
cmb(candidates, target, v, i + , sum + candidates[i], res);
v.pop_back();
}
}
}
};

LeetCode 40. 组合总和 II(Combination Sum II)的更多相关文章

  1. LeetCode 39. 组合总和(Combination Sum)

    题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限 ...

  2. Java实现 LeetCode 40 组合总和 II(二)

    40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在 ...

  3. [Leetcode 40]组合数和II Combination Sum II

    [题目] Given a collection of candidate numbers (candidates) and a target number (target), find all uni ...

  4. 【Leetcode】【Medium】Combination Sum II

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

  5. 【leetcode刷题笔记】Combination Sum II

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

  6. [Swift]LeetCode40. 组合总和 II | Combination Sum II

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

  7. leetcode 40. 组合总和 II (python)

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...

  8. 【LeetCode每天一题】Combination Sum II(组合和II)

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

  9. [Swift]LeetCode216. 组合总和 III | Combination Sum III

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

随机推荐

  1. Communication between C++ and Javascript in Qt WebEngine(转载)

    Communication between C++ and Javascript in Qt WebEngine admin January 31, 2018 0 As Qt WebKit is re ...

  2. java实现spark常用算子之flatmap

    import org.apache.spark.SparkConf;import org.apache.spark.api.java.JavaRDD;import org.apache.spark.a ...

  3. vue 组件基本使用

    组件的基本使用 注册组件 注册组件就是利用Vue.component()方法,先传入一个自定义组件的名字,然后传入这个组件的配置.vue.component()注册的全局组件 Vue.componen ...

  4. javaee 自定义标签实战

    用过javaee标准标签库的里的标签应该都知道,标签的存在使得页面上的jsp脚本大大减少,甚至说没有了,大大提高了工作效率,使得页面的整洁性也有了很大的提高.下面我们就 模仿核心标签库中choose标 ...

  5. Ubuntu中出现“Could not get lock /var/lib/dpkg/lock”的解决方法

    在运行Ubuntu安装软件,使用命令sudo  apt-get install时,有时会出现以下的错误: E: Could not get lock /var/lib/dpkg/lock - open ...

  6. Python名称空间与作用域

    什么是名称空间? 比如下图的长方形代表内存,在这个内存里我要设置一个变量100,那么这个变量得有个门牌号,就叫他a吧,但其实变量不是这么存储的,他其实的存储方式是这样的,他会有一个名称空间,这个名称空 ...

  7. 文件hash、上传,实现文件上传重复验证

    在平台开发中,我们往往对性能要求十分严苛,每一个字段.接口都有严格的要求. 系统中文件流操作十分占用资源,这里为大家介绍对文件上传进行哈希校验---同一文件只允许上传一次到服务器,其他的上传只要指向文 ...

  8. iptables 设置指定IP客户端访问服务器redis端口

    一.需求描述 服务器172.28.18.75开放了6379redis端口,由于没有设置登录密码,所以需要防火墙设置只能指定的IP地址172.28.5.125客户端访问redis端口 二.查看172.2 ...

  9. Cuda9.0安装

    CUDA 9.0安装笔记 最近实验室新购买两块K80的GPU.作为好奇的小猪,当然会自报奋勇去配置环境.在这篇博客中将会介绍在centos7下配置CUDA 9.0的步骤. 1. 什么是CUDA? 引用 ...

  10. java多线程的四种实现方式

    主要有四种:继承Thread类.实现Runnable接口.实现Callable接口通过FutureTask包装器来创建Thread线程.使用ExecutorService.Callable.Futur ...