一个数可以使用多次

图:

  节点:x(当前的和,当前要考虑的数a[i])

  边:x->

    y1(当前的和,下一个要考虑的数a[i+1])

    y2(当前的和+a[i],下一个要考虑的数a[i+1])

BFS

  如何求具体解?

  队列里放全部的“部分解”——浪费空间

  每个节点存放到它的前一个节点——最终还要计算解

DFS

  会好一些

leetcode 77,78,90:枚举全部子集

leetcode 51,52:n皇后问题

  1. class Solution {
  2. public:
  3. void help(vector<int> &a, int now, int sum, int target, vector<int> &path, vector<vector<int> > &ans) {
  4. if (sum > target) {
  5. return ;
  6. }
  7. if (now >= a.size()) {
  8. if (sum == target) {
  9. ans.push_back(path);
  10. }
  11. return ;
  12. }
  13. if ((now == ) || (a[now - ] != a[now])) {
  14. path.push_back(a[now]);
  15. help(a, now, sum + a[now], target, path, ans);
  16. path.pop_back();
  17. }
  18. help(a, now + , sum, target, path, ans);
  19. }
  20. /**
  21. * @param candidates: A list of integers
  22. * @param target:An integer
  23. * @return: A list of lists of integers
  24. */
  25. vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
  26. // write your code here
  27. sort(candidates.begin(), candidates.end());
  28. vector<int> path;
  29. vector<vector<int> > ans;
  30. help(candidates, , , target, path, ans);
  31. return ans;
  32. }
  33. };

LintCode: Combination Sum的更多相关文章

  1. LintCode: Combination Sum II

    C++ DFS class Solution { public: void help(vector<int> &a, int now, int sum, int target, v ...

  2. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  3. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  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 组合之和

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

  6. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  7. LeetCode:Combination Sum I II

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  8. Combination Sum | & || & ||| & IV

    Combination Sum | Given a set of candidate numbers (C) and a target number (T), find all unique comb ...

  9. 【leetcode】Combination Sum II

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

随机推荐

  1. 报错:无法截断表 '某表',因为该表正由 FOREIGN KEY 约束引用

    某表的某个字段作为另一个表的FOREIGN KEY,在truncate另外一个表后,再truncate某表,就报如上的错. 解决方法: → 删除另外一个表的外键 IF OBJECT_ID(N'[dbo ...

  2. jQuery把所有被选中的checkbox的某个属性值连接成字符串

    有这样的一个需求:对于一组checkbox,当点击每个checkbox后,把当前处于选中状态的checkbox的某个属性值取出来连接成字符串,并以逗号分开. html部分: <input typ ...

  3. 再谈vc发送键盘、组合键消息

    关于向Windows窗口发送Alt组合键的问题,这个真是经典问题啊,在网上找了一下,问的人N多,方法差不多, 但就是没有很好解决问题. 之前找到一个能正确发送的code:(Alt+A) PostMes ...

  4. iOS: 计算 UIWebView 的内容高度

    - (void)webViewDidFinishLoad:(UIWebView *)wb { //方法1 CGFloat documentWidth = [[wb stringByEvaluating ...

  5. DotNetty 学习

    [转载]http://www.cnblogs.com/littlegod/p/7699482.html DotNetty的学习是带着如下这些问题展开: 1. Socket基础框架方案: 通信模式:异步 ...

  6. 【转】比较init-method,afterPropertiesSet和BeanPostProcessor

    一.简单介绍 1.init-method方法,初始化bean的时候执行,可以针对某个具体的bean进行配置.init-method需要在applicationContext.xml配置文档中bean的 ...

  7. Ioc:The basic pattern for integrating Autofac into your application

    The basic pattern for integrating Autofac into your application is: Structure your app with inversio ...

  8. linux dig命令 转

    dig 命令主要用来从 DNS 域名服务器查询主机地址信息. 查询单个域名的 DNS 信息 dig 命令最典型的用法就是查询单个主机的信息. $ dig baidu.com dig 命令默认的输出信息 ...

  9. 神探夏洛克第一季/全集Sherlock1迅雷下载

    第一季 Sherlock Season 1 (2010)看点:夏洛克·福尔摩斯(Sherlock Holmes)是一个虚构的侦探人物,是由19世纪末的英国侦探小说家阿瑟·柯南·道尔所塑造的一个才华横溢 ...

  10. Android之TextView部分颜色变动

    public class StringHandleExampleActivity extends Activity { /** Called when the activity is first cr ...