这两道题的基本思路和combination那一题是一致的,也是分治的方法。

其中combination Sum复杂一点,因为每个数可能用多次。仔细分析下,本质上也是一样的。原来是每个数仅两种可能。现在每个数有k +1中可能,k = target / i。

所以就是把简单的if else 分支变成for循环:

vector<vector<int> > combHelper(vector<int>::iterator first,
vector<int>::iterator last, int target){
vector<vector<int>> result;
if (target <= 0 || last == first) return result;
if (*(last - 1) == target){
vector<int> vi;
vi.push_back(target);
result.push_back(vi);
auto r2 = combHelper(first, last - 1, target);
for (auto it = r2.begin(); it != r2.end(); it++)
result.push_back(*it);
}
else if (*(last - 1) < target){
auto r1 = combHelper(first, last - 1, target - *(last - 1));
for (auto it = r1.begin(); it != r1.end(); it++){
it->push_back(*(last - 1));
result.push_back(*it);
}
auto r2 = combHelper(first, last - 1, target);
for (auto it = r2.begin(); it != r2.end(); it++)
result.push_back(*it);
}
else {
auto r2 = combHelper(first, last - 1, target);
for (auto it = r2.begin(); it != r2.end(); it++)
result.push_back(*it);
}
return result;
}
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
sort(num.begin(), num.end());
auto result = combHelper(num.begin(), num.end(), target);
if (!result.empty()){
sort(result.begin(), result.end());
auto it = unique(result.begin(), result.end());
result.erase(it, result.end());
}
return result;
}

  

Combination Sum 和Combination Sum II的更多相关文章

  1. 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV

    ▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...

  2. c++谭浩强教材教学练习例题1.2 求两数之和 为什么sum=a+b;sum的值为65538

    第一章 #include <iostream>using namespace std; int main(){ int a,b,sum; sum=a+b; cin>>a> ...

  3. leetcode-combination sum and combination sum II

    Combination sum: Given a set of candidate numbers (C) and a target number (T), find all unique combi ...

  4. UVALive8518 Sum of xor sum

    题目链接:https://vjudge.net/problem/UVALive-8518 题目大意: 给定一个长度为 $N$ 的数字序列 $A$,进行 $Q$ 次询问,每次询问 $[L,R]$,需要回 ...

  5. Leetcode40. Combination Sum组合总数2 II

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

  6. [leetcode] Combination Sum and Combination SumII

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

  7. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  8. 1. Two Sum + 15. 3 Sum + 16. 3 Sum Closest + 18. 4Sum + 167. Two Sum II - Input array is sorted + 454. 4Sum II + 653. Two Sum IV - Input is a BST

    ▶ 问题:给定一个数组 nums 及一个目标值 target,求数组中是否存在 n 项的和恰好等于目标值 ▶ 第 1题,n = 2,要求返回解 ● 代码,160 ms,穷举法,时间复杂度 O(n2), ...

  9. Python神坑:sum和numpy.sum

    同样的一段代码,在两个python文件里面执行的结果不一样,一个是按照列单位进行sum一个是所有元素进行sum: def distCal(vecA, vecB): return sqrt(sum(po ...

随机推荐

  1. 封装jquery时用到的东西

    顺序都是瞎拍的,就是明显分割用 1.将函数封装成$(' ')这种形式 把函数名起成$ $(各种选择器) $(selector) 2.有时候jquery可以继续加点,返回自己本身的元素 创建个构造函数, ...

  2. ubuntu下python3安装scrapy,OpenSSL

    环境:ubuntu 16.04  ,  python3.5.1+ 安装顺序如下: sudo apt-get install build-essential sudo apt-get install p ...

  3. Best Time to Buy and Sell Stock II [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ Basic idea: ...

  4. C++格式化输入输出

    要实现格式化输入输出,程序需要包含 iostreams 标准标头 <iomanip> 以定义几个各自采用单个参数的操控器. 备注: 其中每个操控器都返回重载 basic_istream&l ...

  5. 超详细的Xcode代码格式化教程,可自定义样式。

    超详细的Xcode代码格式化教程,可自定义样式. 为什么要格式化代码 当团队内有多人开发的时候,每个人写的代码格式都有自己的喜好,也可能会忙着写代码而忽略了格式的问题.在之前,我们可能会写完代码后,再 ...

  6. mouseover 和 hover 方法

    mouseover 和 hover 方法的不同?jquery   hover包括了鼠标移到对象上,同时鼠标再移出对象的过程,相应的子类也被选中. mouseover是鼠标经过对象时,不包含他的子类同时 ...

  7. Hdu4349 Xiao Ming's Hope

    Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. 使用Yeoman,Grunt和Bower开发AngularJS(译)

    使用Yeoman产生AngularJS的主要骨架 使用Grunt加速开发和帮助执行 使用Bower来加入第三方插件和框架——third party plugins/frameworks 一.准备工作 ...

  9. 你所知道的Java单例模式并不是单例模式

    当我们搜索单例模式的时候,能看到很多例子,什么懒汉式.饿汉式,大概如下: public class Singleton { private static Singleton instance=null ...

  10. Icon资源详解[1]

    本文分享&备忘最近了解到的icon资源在windows平台下相关的一部分知识,所有测试代码都尽可能的依赖win32 API实现.更全面的知识,参考文末列出的”参考资料“.      关键字:I ...