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 ...
随机推荐
- 用apache commons-pool2建立thrift连接池
Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架.具体的介绍可以看Apache的官方网站:http://thrift.apache.org/ . ...
- HDU4089(概率dp)
题解 要点: 1.转移方程分三段,这个……有点复杂但是还好吧……大概就是求啥设啥,然后只通过可行的状态过来.在纸上记一记. 2.每层里面必须先求dp[i][i],简直就是我求我自己……用类似进制数那种 ...
- 洛谷 P1094 纪念品分组
P1094 纪念品分组 先按价格对纪念品排序(这里是从大到小),然后从两端向中心开始配对,有两个变量i和j,表示正在处理的两个纪念品编号,开始时i=1,j=n,如果a[i]+a[j]>w则第i贵 ...
- a标签中href=""的几种用法
http://blog.csdn.net/u010297791/article/details/52784879 这是分页上的 <?php function pages($page,$e_pag ...
- 《javascript设计模式》笔记之第四章:继承
一:首先,一个简单的继承实例: 首先是创建一个父类Person: function Person(name) { this.name = name; } Person.prototype.getNam ...
- nodejs express session用法(含保存到redis)
普通用法: var express = require('express'); var session = require('express-session'); var app = express( ...
- [转]Android应用自动更新功能的代码实现
本文转自:http://www.cnblogs.com/coolszy/archive/2012/04/27/2474279.html 由于Android项目开源所致,市面上出现了N多安卓软件市场.为 ...
- 09通过winfrom实现简单的播放音、视频
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- React 实践记录 01 组件开发入门
Introduction 本文组成: Ryan Clark文章Getting started with React的翻译. 博主的实践心得. React由Facebook的程序员创建,是一个非常强大的 ...
- openssl 安装配置
Openssl是个为网络通信提供安全及数据完整性的一种安全协议,囊括了主要的密码算法.常用的密钥和证书封装管理功能以及SSL协议,并提供了丰富的应用程序供测试或其它目的使用.首先下载Openssl包: ...