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 ...
随机推荐
- 浅析java中clone()方法
本文转载自:http://blog.csdn.net/mengxiangyue/article/details/6818611 Java中我们可能都遇到过这样的情况,在我们将一个对象做为参数传给一个函 ...
- 使用xpath爬取猫眼电影排行榜
最近在学习xpath,在网上找资料的时候,发现一个新手经常拿来练手的项目,爬取猫眼电影前一百名排行的信息,很多都是跟崔庆才的很雷同,基本照抄.这里就用xpath自己写了一个程序,同样也是爬取猫眼电影, ...
- python题
1.一行代码实现1--100之和 利用sum()函数求和 sum(range(1,101) 2.如何在一个函数内部修改全局变量 利用global 修改全局变量 3.列出5个python标准库 Pyth ...
- logging:不喜欢写日志可不好哦
logging模块简介 logging模块是python内置的标准模块,主要用于输出程序的运行日志. 可以设置输出日志的等级,日志保存路径,日志文件回滚等等. logging模块的基本使用 impor ...
- 一、PHP和Apache实现多用户自助建站
一.环境搭建准备
- 杜教BM模板
#include<bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #defi ...
- Summer training round2 #3
A!: GTY系列题 B!:莫队加分块 GTY系列题 C!:线段树模拟拓扑排序(把普通的拓扑排序的栈操作改成线段树区间减一,查询区间最右侧的0的位置即可.注意一 ...
- ftp不能复制文件
解决办法: ie->工具->internet选项->安全->自定义级别->下载->文件下载->启用
- HihoCoder1336 Matrix Sum(二维树状数组求和)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You are given an N × N matrix. At the beginning every element ...
- Django—ajax、前端后端编码格式,bulk_create批量插入语数据库、自定义分页
一.ajax简介: XML也是一门标记语言该语法应用场景 1.写配置文件 2.可以写前端页面(odoo框架中 erp) 每家公司都会有属于这家公司独有的内部管理软件:专门用来开发企业内部管理软件 框架 ...