一个数可以使用多次

图:

  节点: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 == 0) || (a[now - 1] != 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 + 1, 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, 0, 0, target, path, ans);
31         return ans;
32     }
33 };

转载于:https://www.cnblogs.com/CheeseZH/p/5012803.html

LintCode: Combination Sum相关推荐

  1. 36 数字组合(Combination Sum)

    文章目录 1 题目 2 解决方案 2.1 思路 2.2 图解 2.3 时间复杂度 2.4 空间复杂度 3 源码 1 题目 题目:数字组合(Combination Sum) 描述:给定一个候选数字的集合 ...

  2. Lintcode: k Sum II

    Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where the ...

  3. 40. Combination Sum II 组合总和 II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...

  4. 216. Combination Sum III

    /** 216. Combination Sum III * 2016-6-12 by Mingyang* i一定要取到9,虽然大小聪明,想只取到7,但是后面的遍历可能也会遍历到9啊.* 1.长度标准 ...

  5. Combination Sum 和Combination Sum II

    这两道题的基本思路和combination那一题是一致的,也是分治的方法. 其中combination Sum复杂一点,因为每个数可能用多次.仔细分析下,本质上也是一样的.原来是每个数仅两种可能.现在 ...

  6. 【动态规划】LeetCode 377. Combination Sum IV

    LeetCode 377. Combination Sum IV Solution1: 我的未能AC的答案 题目描述的和前几道题差不多,但实际上不能用DFS来做(会超时),要用动态规划,还是记录一下吧 ...

  7. 【DFS】LeetCode 39. Combination Sum

    LeetCode 39. Combination Sum Solution1: DFS,这个套路要熟记啊! class Solution { public:vector<vector<in ...

  8. LC39 Combination Sum

    还是利用深搜的思想,注意一个元素可以取无数次.而LC40 Combination Sum II 就有次数限制,有次数限制的情况下可以先判断一个数是否与它前面的数相等,若相等则跳过该元素,直到找到一个数 ...

  9. 【LeetCode】#39组合总和(Combination Sum)

    [LeetCode]#39组合总和(Combination Sum) 加粗样式 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数 ...

  10. lintcode: k Sum 解题报告

    K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...

最新文章

  1. pandas dataframe数据聚合groupby、agg、privot基于sum统计详解及实例
  2. WebService调用(基于KSOAP2)
  3. python验证中心极限定理_我竟然混进了Python高级圈子!
  4. 怎么检查python是否安装成功-检查python以及django是否安装配置成功
  5. socket的NIO操作
  6. OpenFOAM程序开发的基本知识(基本术语)
  7. Ubuntu14.04 + KinectV2驱动安装 以及 Ros接口(基于网上方法试错 改进版)
  8. Docker安装与卸载,配置阿里云镜像加速器
  9. python中dir用法_Python dir()函数
  10. c语言字符串去除第一个和最后一个_387. 字符串中的第一个唯一字符
  11. Linux下生产者与消费者问题
  12. python中tolist()功能
  13. 什么软件画er图方便_如何画好ER图
  14. 找不到项目 该项不在计算机中,删除文件夹提示找不到该项目怎么删除?“找不到该项目”强删方法(图文)...
  15. pubg微信登录服务器维护,全军出击微信登录不了怎么办
  16. 神策数据桑文锋:重构数据根基,实现数字化经营
  17. 通过GB28181实现对安防摄像头的直播回放控制
  18. 重磅:阿里开启大规模校招,传已启动保密项目
  19. 关于计算几何一些算法
  20. 2022/06/14,15 day15与day16:内部类

热门文章

  1. 如何关闭MyEclipse自动更新
  2. (已解决)Mon Apr 08 14:02:29 CST 2019 WARN: Establishing SSL connection without server's
  3. JavaScript实现监听移动端上下左右滑动事件
  4. Windows中使用http-server搭建一个本地服务
  5. MySQL 浅谈NOT NULL和DEFAULT的关系
  6. 04. Make sure that objects are initialized before they're used
  7. mysql权限怎么修改_mysql修改权限
  8. qt 串口粘包_QT C++ TCP大文件高效传输高效 解决粘包问题
  9. jQuery特效:实现抽奖
  10. 实战HTML:登陆界面的实现