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的更多相关文章

  1. LeetCode: Combination Sum II 解题报告

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

  2. [LeetCode] Combination Sum II 组合之和之二

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  3. [LeetCode] Combination Sum II (递归)

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  4. LeetCode——Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  5. LeetCode Combination Sum II (DFS)

    题意: 在集合candidates中选出任意多个元素,使得他们的和为target,返回所有的组合,以升序排列. 思路: 难点在于如何去重,比如集合{1,1,2},target=3,那么只有一个组合就是 ...

  6. leetcode Combination Sum II python

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  7. [Leetcode] combination sum ii 组合之和

    Given a collection of candidate numbers ( C ) and a target number ( T), find all unique combinations ...

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

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

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

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

随机推荐

  1. DOM – (w3school)1.DOM 方法 + 2.DOM属性 + 3.DOM 元素

      1.DOM 方法   一些 DOM 对象方法 这里提供一些您将在本教程中学到的常用方法: 方法 描述 getElementById() 返回带有指定 ID 的元素. getElementsByTa ...

  2. 理解Java中的final和static关键字

    回顾这两个关键字前,先考虑一个问题: Static变量存储在JVM中的位置,或者说static变量是如何被加载的? JVM会把类的静态方法和静态变量在类加载的过程中读入方法区(Method Area) ...

  3. Delphi中exit、break、continue等跳出操作的区别

    Delphi中表示跳出的有break,continue,abort,exit,halt,runerror等 1.break 强制退出最近的一层循环(注意:只能放在循环里:而且是只能跳出最近的一层循环) ...

  4. MyEclipse 选中右侧编辑的文件时自动展开左侧目录树

  5. Bat脚本实现MySQL数据库SQL文件备份

    @echo offecho 在线兑奖系统自动备份脚本(请勿关闭) 联系人:  电话::loopset /a "FDate=%date:~,4%%date:~5,2%%date:~8,2%&q ...

  6. Sizeof运算符小结

    以下内容援引自<C Primer Plus>中文版第五版Page95 Sizeof运算符以字节为单位返回其操作数的大小.(在C中,1个字节被定义为char类型所占用空间的大小.在过去,1个 ...

  7. 单例模式/singleton模式/创建型模式

    Java实现要点: 私有构造方法 线程安全(并发的考虑) 延迟加载(效率的考虑,对于较大的类在使用时在加载) 公有方法访问单一实例 常见单例模式代码及问题 //无延迟加载,常驻内存(即使不使用) cl ...

  8. Android学习二_八:Animation的使用(一) (转)

    一.Animations介绍 Animations是一个实现android UI界面动画效果的API,Animations提供了一系列的动画效果,可以进行旋转.缩放.淡入淡出等,这些效果可以应用在绝大 ...

  9. 用js实现图片自动加载的瀑布流效果

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  10. input上下居中问题

    IE:不管该行有没有文字,光标高度与font-size一致.FF:该行有文字时,光标高度与font-size一致.该行无文字时,光标高度与input的height一致.Chrome:该行无文字时,光标 ...