题目

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;
};

GitHub测试程序源码

LeetCode(40) Combination Sum II的更多相关文章

  1. 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 ...

  2. LeetCode(39) Combination Sum

    题目 Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C w ...

  3. 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 ...

  4. LeetCode(40):组合总和 II

    Medium! 题目描述: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数 ...

  5. LeetCode(90):子集 II

    Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1 ...

  6. LeetCode(219) Contains Duplicate II

    题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...

  7. LeetCode(137) Single Number II

    题目 Given an array of integers, every element appears three times except for one. Find that single on ...

  8. leetcode第39题--Combination Sum II

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

  9. 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 ...

随机推荐

  1. scrapy框架中Item Pipeline用法

    scrapy框架中item pipeline用法 当Item 在Spider中被收集之后,就会被传递到Item Pipeline中进行处理 每个item pipeline组件是实现了简单的方法的pyt ...

  2. [POJ1463] Strategic game

    题目链接: 传送门 题目大意: Bob非常享受玩电脑游戏的过程,尤其是策略游戏,但是在有些时候,他因为不能在第一时间找到最佳的策略而十分伤心. 现在,他遇到了一个问题.他必须保卫一个中世纪的城市,有很 ...

  3. 关于在ARM MDK 中使用STM32F4xx 硬件浮点单元的话题

    http://mp.weixin.qq.com/s/CDyZ8v2kLiyuIBHf7iqEPA

  4. Panoramic Photography

    http://codeforces.com/gym/101149/problem/J 给出n个数字,表示第i条街有a[i]个照片存在过,其中,每个照片可以覆盖一段连续的区间, 就是一张照片可以覆盖[2 ...

  5. spring mvc 获取所有注册的url

    背景:坑货同事写代码一点规范都没有,瞎写,根据url没法直接定位到方法,无奈产生了此接口,程序员何苦为难程序员呢 @Autowired private RequestMappingHandlerMap ...

  6. 实现如下语法的功能:var a = (5).plus(3).minus(6);

    Number.prototype.plus= function(val){ return parseInt(this)+val; }; Number.prototype.minus= function ...

  7. Java中的数据类型——通过示例学习Java编程(5)

    作者:CHAITANYA SINGH 来源:https://www.koofun.com//pro/kfpostsdetail?kfpostsid=15 数据类型用来定义变量可以采用的值,例如,如果变 ...

  8. jquery.qrcode.min.js生成二维码

    jquery.qrcode.min.js是一款可以生成二维码的插件,使用前提是先引入jquery,因为jquery.qrcode.min.js依赖jquery. 基本用法 1.引入js <scr ...

  9. (五)mybatis之下载与基本构成

    1.  下载MyBatis. 输入网址:https://github.com/mybatis/mybatis-3/releases 进入Mybatis下载页面,选择第一个选项,然后就可以加载到myba ...

  10. JSON数组不用字符串转换的写法

    var organization = []; //机构组织 //初始化用户数据列表中用户机构列的数据源 admin.ajax("GetOrganizationInfo", null ...