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 toT.
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:
vector<vector<int> > res;
vector<int> path;
vector<int> candidates;
int target; void solve(int start, int sum){
if(sum > target) return;
if(sum == target){
if(find(res.begin(),res.end(),path)==res.end())
res.push_back(path);
return;
}
for(int i = start; i < candidates.size(); ++ i){
path.push_back(candidates[i]);
solve(i+,sum+candidates[i]);
path.pop_back();
}
} vector<vector<int> > combinationSum2(vector<int> &candidates, int target) {
this->candidates = candidates;
this->target = target;
sort(this->candidates.begin(),this->candidates.end());
solve(,);
return res;
}
};
Leetcode 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 ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Combination Sum II (递归)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode——Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode Combination Sum II (DFS)
题意: 在集合candidates中选出任意多个元素,使得他们的和为target,返回所有的组合,以升序排列. 思路: 难点在于如何去重,比如集合{1,1,2},target=3,那么只有一个组合就是 ...
- leetcode Combination Sum II python
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [Leetcode] combination sum ii 组合之和
Given a collection of candidate numbers ( C ) and a target number ( T), find all unique combinations ...
- [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 ...
随机推荐
- markdown编辑器使用建议
markdown在线编辑器: https://stackedit.io/editorhttp://dillinger.io/ windows 下建议使用 MarkdownPad linux 下建议使用 ...
- poj 1701【数学几何】
The area Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- 学习SQLAlchemy Core
有时间了就要慢慢看,死守DJANGO ORM,明显没有SQLAlchemy有优势. 因为SQLAlchemy针对整个PYTHON都是有用的. 找了本书,慢慢撸. <Essential.SQLAl ...
- HTML5+CSS3的响应式网页设计:自动适应屏幕宽度
这几天都在修改博客上面的样式.本来用的是d83.0的模板.自己又修改了许多地方,其中自己修改的一些地方在手机里面显示的效果不是很理想,于是想改成自适应的效果.对CSS3不是特别的熟练,只能去网上找找案 ...
- [LeetCode] Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- Oracle【IT实验室】数据库备份与恢复之三:OS备份/用户管理的备份与恢复
用户管理的备份与恢复也称 OS物理备份,是指通过数据库命令设置数据库为备份 状态,然后用操作系统命令,拷贝需要备份或恢复的文件.这种备份与恢复需要用户的 参与手工或自动完成. 对于使用 OS拷贝备份的 ...
- phpcms v9最常用的22个调用代码
新源网络工作室友情总结phpcms v9最常用的22个调用代码: 调用最新文章,带所在版块{pc:get sql="SELECT a.title, a.catid, b.catid, b.c ...
- WPF QuickStart系列之线程模型(Thread Model)
这篇博客将介绍WPF中的线程模型. 首先我们先来看一个例子,用来计算一定范围内的素数个数. XAML: <Grid> <Grid.RowDefinitions> <Row ...
- @property中strong跟weak的区别
strong关键字与retain关似,用了它,引用计数自动+1,用实例更能说明一切 @property (nonatomic, strong) NSString *string1; @property ...
- php 常见的问题
1. this指针错误的引用变量($(php)<->*(c)) $this->inputData right wrong 2. json_encode(array) 不一定按数组关键 ...