LeetCode(40) 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 (a1, a2, … , 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]
分析
与上一题39 Combination Sum本质相同,只不过需要注意两点:每个元素只能出现结果序列中一次,结果序列不可重复。
只需利用find函数添加一个判重即可。
AC代码
class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
if (candidates.empty())
return vector<vector<int> >();
sort(candidates.begin(), candidates.end());
ret.clear();
vector<int> tmp;
combination(candidates, 0, tmp, target);
return ret;
}
void combination(vector<int> &candidates, int idx, vector<int> &tmp, int target)
{
if (target == 0)
{
if (find(ret.begin(), ret.end(), tmp) == ret.end())
ret.push_back(tmp);
return;
}
else{
int size = candidates.size();
for (int i = idx; i < size; ++i)
{
if (target >= candidates[i])
{
tmp.push_back(candidates[i]);
combination(candidates, i + 1, tmp, target - candidates[i]);
tmp.pop_back();
}//if
}//for
}//else
}
private:
vector<vector<int> > ret;
};
LeetCode(40) Combination Sum II的更多相关文章
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(39) Combination Sum
题目 Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C w ...
- Leetcode 39 40 216 Combination Sum I II III
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- LeetCode(40):组合总和 II
Medium! 题目描述: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数 ...
- LeetCode(90):子集 II
Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1 ...
- LeetCode(219) Contains Duplicate II
题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- leetcode第39题--Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- LeetCode(307) Range Sum Query - Mutable
题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...
随机推荐
- Technocup 2017 - Elimination Round 1 (Unofficially Open for Everyone, Rated for Div. 2) D
The organizers of a programming contest have decided to present t-shirts to participants. There are ...
- 095 Unique Binary Search Trees II 不同的二叉查找树 II
给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种?例如,给出 n = 3,则有 5 种不同形态的二叉查找树: 1 3 3 2 1 ...
- html 手机端click 事件延迟问题(fastclick.js使用方法)
下载地址: fastclick.js 为什么存在延迟? 从点击屏幕上的元素到触发元素的 click 事件,移动浏览器会有大约 300 毫秒的等待时间.为什么这么设计呢? 因为它想看看你是不是要进行双击 ...
- 使用Spring Cloud Feign
使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务 在spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就 ...
- cookie和session基础以及在Django中应用
看了会视频,终于搞懂了~ 1.cookie cookie:保存状态 cookie的工作原理是:由服务器产生内容,浏览器收到请求后保存在本地:当浏览器再次访问时,浏览器会自动带上cookie,这样服务器 ...
- 洛谷 P1474 货币系统 Money Systems
P1474 货币系统 Money Systems !! 不是noip2018的那道题. 简单的多重背包的变式. #include <iostream> #include <cstdi ...
- cf1028C. Rectangles(前缀和)
题意 给出$n$个矩形,找出一个点,使得至少在$n$个矩阵内 Sol 呵呵哒,昨天cf半夜场,一道全场切的题,我没做出来..不想找什么理由,不会做就是不会做.. 一个很显然的性质,如果存在一个点 / ...
- APP产品体验
一.前言 1.背景介绍 体验人员:羽珞体验时间:2016.4.12~2016.4.14 2.体验环境 产品名称 产品版本 测试设备 设备系统 易助(ehlep) 1.0 TCL J738M A ...
- Objective-C Composite Objects
We can create subclass within a class cluster that defines a class that embeds within it an object. ...
- Mysql常用的优化技巧
1.通过show status 命令了解各种sql的执行效率 2. 定位执行效率较低的SQL语句 开启慢查询记录: 打开Mysql配置文件my.ini ,找到 [mysqld] 在其下面添加 long ...