LintCode: Combination Sum II
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的更多相关文章
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- 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 ...
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- Lintcode: k Sum II
Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where the ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- 【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 ...
- LeetCode: Combination Sum II 解题报告
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- 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) ...
随机推荐
- Object [object Object] has no method 'live'
用了2个jquery的2个文件: <script src="~/Scripts/jquery-1.10.2.js"></script> <script ...
- java根据模板导出PDF详细教程
原文:https://blog.csdn.net/pengyufight/article/details/75305128 题记:由于业务的需要,需要根据模板定制pdf文档,经测试根据模板导出word ...
- 下载java生成PDF
/** * 下载打印PDF * @param request * @param response * @throws ServletException * @throws IOException * ...
- JavaScript 判断输入是否为中文的函数
//---------------------------------------------------------- // 功能:判断输入是否为中文的函数 // 参数: // s // 返回值: ...
- python测试开发django-39.xadmin详情页面布局form_layout
前言 xadmin的详情页面默认是一行展示一个字段,可以使用form_layout对详情页面的布局重新设计. 可以设置必填和非必填字段,也可以设置不显示,不可以编辑的字段. models模块 先在mo ...
- Linux线程优先级
转自:https://www.cnblogs.com/imapla/p/4234258.html Linux内核的三种调度策略: 1.SCHED_OTHER 分时调度策略 2.SCHED_FIFO ...
- Java中判断String不为空的问题性能比较
function 1: 最多人使用的一个方法, 直观, 方便, 但效率很低. function 2: 比较字符串长度, 效率高, 是我知道的最好一个方法. function 3: Java SE 6. ...
- Hyperledger 项目
https://github.com/hyperledger/fabric.githttps://github.com/hyperledger/blockchain-explorer.githttps ...
- ListView取消和自定义分割线的方法
一.不显示分割线 XML android:footerDividersEnabled="false"即可. JAVA mListView.setDivider(null); 二.改 ...
- Java反编译工具Jad详解
做项目过程中需要反编译一个jar包,于是作了一些学习,记录下来. Jad(JAva Decompiler)是一个Java的反编译器,可以通过命令行把Java的class文件反编译成源代码. 如果你在使 ...