Combination Sum 和Combination Sum II
这两道题的基本思路和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的更多相关文章
- 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV
▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
- c++谭浩强教材教学练习例题1.2 求两数之和 为什么sum=a+b;sum的值为65538
第一章 #include <iostream>using namespace std; int main(){ int a,b,sum; sum=a+b; cin>>a> ...
- 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 ...
- UVALive8518 Sum of xor sum
题目链接:https://vjudge.net/problem/UVALive-8518 题目大意: 给定一个长度为 $N$ 的数字序列 $A$,进行 $Q$ 次询问,每次询问 $[L,R]$,需要回 ...
- Leetcode40. Combination Sum组合总数2 II
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...
- [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 ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...
- 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), ...
- Python神坑:sum和numpy.sum
同样的一段代码,在两个python文件里面执行的结果不一样,一个是按照列单位进行sum一个是所有元素进行sum: def distCal(vecA, vecB): return sqrt(sum(po ...
随机推荐
- iOS应用架构谈 组件化方案
转载: iOS应用架构谈 组件化方案 简述 前几天的一个晚上在infoQ的微信群里,来自蘑菇街的Limboy做了一个分享,讲了蘑菇街的组件化之路.我不认为这条组件化之路蘑菇街走对了.分享后我私聊了Li ...
- 自己动手做jQuery插件
前言:这种东西随意可以在网上收到,这里我还是只是记下自己的见解和领. 第一种方式 (function ($) { $.extend({ sayHello: function (name) { aler ...
- HTML5自学笔记[ 19 ]canvas绘图实例之炫彩时钟
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- POJ--2158--------------Milking Grid(最小覆盖字符矩阵)---(开二维kmp)
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6169 Accepted: 2573 Desc ...
- C语言指针(一)
一.指针 定义指针变量 指针指向的数据类型 *指针变量名称; 例: int *p; *作用: 1.在定义变量的时候 * 是一个类型说明符,说明定义的这个变量是一个指针变量 2.在不是定义变量的时候 * ...
- easyui layout 收缩的bug
easyui layout提供collapse方法折叠指定的 panel,'region' 参数可能的值是:'north'.'south'.'east'.'west',但是在 IE6的环境下,调用这个 ...
- mysqldump使用语法
复制代码 代码如下: mysqldump -u user -p db tab1 tab2 > db.sql 恢复 复制代码 代码如下: mysql -u user -p db < db ...
- [转]使用maven镜像
综述 用maven做项目,最郁闷的莫过于某些依赖库下载不了.被墙了,你懂的.使用maven镜像仓库及其重要,特别是国内的镜像,可以有效缓解被墙疼痛. 常用的镜像 国外镜像 ibiblio.org &l ...
- UVa 11426 - GCD - Extreme (II)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- Arithmetic Progressions
题目大意: 求出满足条件A的等差数列: A:长度为N(N<=25),每个元素都能表示成 两个数p,q的平方和(0<=p,q<=m<=250): 解题过程: 1.处理出所有的能拆 ...