LeetCode 40. 组合总和 II(Combination Sum II)
题目描述
给定一个数组 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)的更多相关文章
- LeetCode 39. 组合总和(Combination Sum)
题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限 ...
- Java实现 LeetCode 40 组合总和 II(二)
40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在 ...
- [Leetcode 40]组合数和II Combination Sum II
[题目] Given a collection of candidate numbers (candidates) and a target number (target), find all uni ...
- 【Leetcode】【Medium】Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【leetcode刷题笔记】Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [Swift]LeetCode40. 组合总和 II | Combination Sum II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- leetcode 40. 组合总和 II (python)
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...
- 【LeetCode每天一题】Combination Sum II(组合和II)
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [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 ...
随机推荐
- 关于ionic4导入android studio的注意事项
最近看IT营的视频的时候,发现视频讲解的打包真是轻松的不得了,但是当自己导入打包的时候,你就会发现,没有最坑,只有更坑,按照教程来打包,估计你这辈子很难还原成功的,下面就来说一下关于 gradle与g ...
- 105、Replicated Mode VS Global Mode (Swarm12)
参考https://www.cnblogs.com/CloudMan6/p/8028712.html Swarm 可以在 Service 创建和运行过程中灵活的通过 --replicas 调整容器 ...
- 一个java文件中有几个类,编译后有几个class文件?
在一个源文件中用class关键字定义了几个类,编译的时候就会产生几个字节码文件
- win7(64位旗舰版)visual studio 2017无法安装及vs2015闪退问题解决方式
折腾了两天,几乎试了网上说的所有方法(就差重装系统了,看到有人说重装系统之后还是同样的问题,果断放弃重装),visual studio 2017的安装问题终于解决了,为了帮助同样还在折腾的初级开发者们 ...
- LRU算法介绍和在JAVA的实现及源码分析
一.写随笔的原因:最近准备去朋友公司面试,他说让我看一下LRU算法,就此整理一下,方便以后的复习. 二.具体的内容: 1.简介: LRU是Least Recently Used的缩写,即最近最少使用. ...
- Delphi 集合类型
- 标准C语言(12)
一个存储区的地址应该是它自身大小的整数倍(双精度浮点类型存储区的地址只需要是4的整数倍),这个规则叫数据对齐,结构体内部的存储区通常也需要遵守数据对齐的规则,数据对齐有可能导致结构体相邻子存储区之间有 ...
- dpkg -i libequinox-osgi-java_3.8.1-8_all.deb
dpkg -i libequinox-osgi-java_3.8.1-8_all.deb dpkg -i libequinox-osgi-java_3.8.1-8_all.deb https://ww ...
- [uboot] (第四章)uboot流程——uboot编译流程 (转)
以下例子都以project X项目tiny210(s5pv210平台,armv7架构)为例 [uboot] uboot流程系列:[project X] tiny210(s5pv210)上电启动流程(B ...
- Linux文件类型和文件相关命令
文件类型 ll后可以看到文件详情: -:常规文件(内部类型是什么,用file命令) d:directory,目录文件 b:blobk device,块设备文件,支持以“block”为单位进行随机访问 ...