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], ...
随机推荐
- HASH JOIN算法
哈希连接(HASH JOIN) 前文提到,嵌套循环只适合输出少量结果集.如果要返回大量结果集(比如返回100W数据),根据嵌套循环算法,被驱动表会扫描100W次,显然这是不对的.看到这里你应该明白为 ...
- vimrc 留备份
set encoding=UTF-8 "encode with UTF-8"set backspace=2set nusyn onset ai!syntax enablesynta ...
- puppet aix package 之rsync安装
AIX中使用RPM安装RSync遇到的问题及解决办法 最近在折腾AIX的系统,它里面本来有一个包管理工具叫installp,但是俺不会用,也不知道从那里找包. 幸亏AIX提供了RPM的支持,所以安装软 ...
- MYSQL数据备份与还原学习笔记
数据备份与还原 1.mysqldump 1.1 文件地址: E:\xampp\mysql\bin 文件名:mysqldump.exe CMD下进入mysqldump.exe cd E:\xampp ...
- Highcharts 基本曲线图
基本曲线图实例 文件名:highcharts_line_basic.htm <html> <head> <meta charset="UTF-8" / ...
- Javascript 精髓整理篇之三(数组篇)postby:http://zhutty.cnblogs.com
今天讲js的数组.数组是js中最基础的数据结构了. 主要讲讲数组实现栈,队列以及其他的基本操作.栈和队列都可以在数组头尾位置处理,所以,都有两种方式. 属性 1.length : 长度,表示数组元素的 ...
- 魅蓝Note有几种颜色 魅蓝Note哪个颜色好看
http://www.pc841.com/shoujizhishi/39882.html http://news.mydrivers.com/1/359/359443.htm 魅族发布首款千元高性价比 ...
- easyui 获取cloumns字段
var colums=datagrid.datagrid('options').columns; var frozens=datagrid.datagrid('options').frozenColu ...
- Java 将自己定义的对象作为HashMap的key
须要继承Map的equals函数和hashCode函数 package com.category; import java.util.HashMap; public class GenCategory ...
- [Typescript] Function defination
Define a function type and params type: // The function init // Accept two params which are both typ ...