题目

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. CF #541div2 F

    题目本质:并查集的链式合并 解决方法1: 类似哈夫曼树,叶节点们为真点,其余造一些虚的父节点,使得dfs这棵树的时候,先进行并查合并的点一定是兄弟节点因而紧挨着被输出,巧妙达到了效果. #pragma ...

  2. 洛谷 P3957 跳房子

    https://www.luogu.org/problemnew/show/P3957 错误记录:1.没开longlong 2. -inf不够小 #include<cstdio> #inc ...

  3. 解决“程序包管理器控制台”输入命令找不到Nuget包问题

    问题: 问题原因: Nuget源的地址上不去 解决办法: 1.将Nuget源更新为可以国内使用的官方Nuget源. 1)打开VS2013:工具-->Nuget程序包管理器-->程序包管理器 ...

  4. 分布式系统ID生成办法

    前言 一般单机或者单数据库的项目可能规模比较小,适应的场景也比较有限,平台的访问量和业务量都较小,业务ID的生成方式比较原始但是够用,它并没有给这样的系统带来问题和瓶颈,所以这种情况下我们并没有对此给 ...

  5. activity间的传参

    Intent有两个作用:激活组件和附带数据 激活另一个activity的方法显示意图:  1.  Intent intent = new Intent();       intent.setClass ...

  6. Jenkins执行shell脚本启动tomcat失败解决方法

    环境:Centos 7 Jenkins版本:2.124 状况:Jenkins会执行服务器某个目录下的Shell, 脚本中功能是复制替换某两个配置文件,然后关闭tomcat,重启Tomcat. 但是,T ...

  7. rsyslog+analyzer

    环境:最小化centos6.2 准备:rsyslog-4.6.1.tar.gz  loganalyzer-3.6.3.tar.gz wget http://download.adiscon.com/l ...

  8. shell中的-z

    -z 字符串为"null",即是指字符串长度为零.

  9. 起学习iOS开发专用词汇

    今天的单词分别是: l   Asynchronous  形容词 异步的 n  副词形式: asynchronously 异步地 n  缩写:ASYNC n  反义词:synchronous 形容词同步 ...

  10. Azure 项目构建 – 构建直播教学系统之媒体服务篇

    本课程主要介绍如何在 Azure 平台上快速构建和部署基于 Azure 媒体服务的点播和直播教学系统, 实践讲解如何使用 Azure 门户创建媒体服务, 配置视频流进行传输,连接 CDN 加速等. 具 ...