LeetCode_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]
class Solution {
public:
void DFS(vector<int> &candidates, int target, int start, int sum, vector<int> &tp){
if(sum == target){
res.push_back(tp);
return;
}
for(int i = start; i< candidates.size(); ++i){
if(i != start && candidates[i] == candidates[i-1])
continue;
if(candidates[i] + sum <= target){
tp.push_back(candidates[i]);
DFS(candidates, target, i+1, sum+candidates[i], tp);
tp.pop_back();
}
}
}
vector<vector<int> > combinationSum2(vector<int> &candidates, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
res.clear();
int len = candidates.size();
if(len < 1 || target <1) return res;
sort(candidates.begin(), candidates.end());
vector<int> tp;
DFS(candidates, target, 0, 0, tp);
return res; }
private:
vector<vector<int>> res;
};
LeetCode_Combination Sum II的更多相关文章
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- [leetcode]Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- 【leetcode】Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- 32. Path Sum && Path Sum II
Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if ...
- LeetCode: Path Sum II 解题报告
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- leetcode2 Two Sum II – Input array is sorted
Two Sum II – Input array is sorted whowhoha@outlook.com Question: Similar to Question [1. Two Sum], ...
随机推荐
- U8800安装软件显示无效的URI问题
看到很多人遇到这个问题,其中包括我自己,最后找到可行的解决办法,现整理出来一个新帖,有同样问题的U友可以参考下. 手机先连接电脑,进入USB存储状态,然后在计算机上找到SD卡目录下的.android_ ...
- HDU-1047(DP-二进制状态压缩)
Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of h ...
- Scala-函数
package com.mengyao.scala.function /** * Scala函数的声明和使用 * * @author mengyao */object Test1 { //主程序的ma ...
- Storm drpc学习
示例代码: package com.lky.test; import org.apache.commons.logging.Log; import org.apache.commons.logging ...
- yii 笔记
Yii1.1: $_GET 可以表示为 Yii::app()->request->getQuery() $_POST 可以表示为 Yii::app()->request->po ...
- (转)Eclipse/Myeclipse 注释注释模板
Window -->preferences --> Java --> Code Style --> Code Templates --> Comments --> ...
- 再探java基础——对面向对象的理解(1)
对象 对象是人们要进行研究的任何事物,从最简单的整数到复杂的飞机等均可看作对象,它不仅能表示具体的事物,还能表示抽象的规则.计划或事件.对象具有属性和行为,在程序设计中对象实现了数据和操作的结合,使数 ...
- 搭建Windows下Java Web开发环境
概要 1.SSH开发相关软件及开发包下载2.软件安装及相关设置3.最简单的Web程序 1.软件下载 在D盘建一个目录JavaTools,用来存放下载的软件和开发包.(本教程将使用D盘,你也可以使用 ...
- 将apk文件添加到Android模拟器(AVD)中运行
apk不同exe和jar文件,apk需要在安卓系统中运行,单有一个apk文件还是没用,不能直接拖进AVD中(当然可以直接放到安卓系统的手机中) 由于我们的eclipse大都是已经安装好,解压直接使用的 ...
- 显示目录树命令tree
-a:显示所有文件,包括隐藏文件 -d:只显示目录 -f:显示完整的文件名,包含路径 -L:显示目录树的深度 [root@rusky /]# tree -L -a -f /home /home |-- ...