C++

DFS

 class Solution {
public:
void help(vector<int> &a, int now, int sum, int target, vector<int> &path, vector<vector<int> > &ans, bool last) {
if (sum > target) {
return ;
}
if (now >= a.size()) {
if (sum == target) {
ans.push_back(path);
}
return ;
}
if ((now == ) || (a[now - ] != a[now]) || last) {
path.push_back(a[now]);
help(a, now + , sum + a[now], target, path, ans, true);
path.pop_back();
}
help(a, now + , sum, target, path, ans, false);
}
/**
* @param num: Given the candidate numbers
* @param target: Given the target number
* @return: All the combinations that sum to target
*/
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
// write your code here
sort(num.begin(), num.end());
vector<int> path;
vector<vector<int> > ans;
help(num, , , target, path, ans, true);
return ans;
}
};

LintCode: Combination Sum II的更多相关文章

  1. 【leetcode】Combination Sum II

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  2. Combination Sum,Combination Sum II,Combination Sum III

    39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...

  3. [Leetcode][Python]40: Combination Sum II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...

  4. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  5. Lintcode: k Sum II

    Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where the ...

  6. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  7. 【LeetCode】40. Combination Sum II (2 solutions)

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  8. LeetCode: Combination Sum II 解题报告

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  9. LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

随机推荐

  1. 报错:TargetException, 非静态方法需要一个目标

    如果实例为null,调用实例方法会报如上错. 解决办法: 检查实例是否为null,考虑什么情况下实例为null,然后排除实例为null的情况.

  2. Javascript数据模型

    本文主要描述Javascript的数据模型,即对Javascript所支持的数据类型的一个全局概缆.文章比较的理论化,非常深入,因此不易理解,但务必对数据模型有一个映象,因为他是理解Javascrip ...

  3. Javascript原型继承原理

    对于面向对象的基础语法在此我就不重复了,对面向对象不熟悉的朋友可以参看<使用面向对象的技术创建高级 Web 应用程序>一文. prototype与[[prototype]] 在有面象对象基 ...

  4. Android 数据存储02之文件读写

    Android文件读写 版本 修改内容 日期 修改人 V1.0 原始版本 2013/2/25 skywang Android文件读写的有两种方式.一种,是通过标准的JavaIO库去读写.另一种,是通过 ...

  5. 在xcode5下设置两个viewController跳转——关键是禁用arc

    1.禁用arc 2.然后使用如下代码: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( ...

  6. 【转】iPhone易被窃听应用三分钟即可获取所有信息

    2011年8月9日10:19 “你有iPhone吗?这下你麻烦了!”昨天香港<东方日报>封面文章用这样的语气报道说,一种iPhone等智能手机窃听程序,正引爆香港. 该报记者亲自试验,发现 ...

  7. byte[]数组的正则表达式搜索 z

    在byte[]数组的特定位置进行正则表达式匹配. 为了从硬盘上搜索特定类型的文件,需要根据文件的特征值进行匹配. 对于已掌握文件结构的文件,采用hard-code的方式进行匹配:这样速度快: 对于未掌 ...

  8. spring boot对输入的字符串进行html转码

    可以使用HtmlUtils这个类进行操作.具体的可以参考API,或者点出来看.

  9. Docker配置国内加速器加速镜像下载的方法

    在搭建hyperledger fabric的开发环境的时候,用docker去下载镜像,好慢好慢,慢到下了一个下午没有下载完成,最后还是失败了.最后去网上找了一下,发现有配置国内加速器加速镜像下载的方法 ...

  10. Dubbo的一些编码约定和设计原则

    编码约定 代码风格 Dubbo 的源代码和 JavaDoc 遵循以下的规范: Code Conventions for the Java Programming Language How to Wri ...