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 ...
随机推荐
- MySQL中基本的多表连接查询教程
一.多表连接类型1. 笛卡尔积(交叉连接) 在MySQL中可以为CROSS JOIN或者省略CROSS即JOIN,或者使用',' 如: SELECT * FROM table1 CROSS JOIN ...
- 根据List中对象的某一属性进行排序
不多说,直接看代码: package test; import java.util.ArrayList; import java.util.Collections; import java.util. ...
- Compound Interest Calculator2.0
Compound Interest Calculator2.0 1.如果按照单利计算,本息又是多少呢? 2.假如30年之后要筹措到300万元的养老金,平均的年回报率是3%,那么,现在必须投入的本金是多 ...
- arguments的基本用法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- (06)odoo报表
----------更新时间:18:06 2016-09-18 星期日18:13 2016-04-05 星期二10:31 2016-03-01 星期二----------* odoo8 采用是Qweb ...
- 2014---多校训练一(A Couple doubi)
Couple doubi Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- LDM和STM指令
LDM批量加载/STM批量存储指令可以实现一组寄存器和一块连续的内存单元之间传输数据. 允许一条指令传送16个寄存器的任意子集和所有寄存器,指令格式如下: LDM{cond} mode Rn{!} ...
- RAID5和RAID10,哪种RAID更适合你(上)
[IT168 专稿]存储是目前IT产业发展的一大热点,而RAID技术是构造高性能.海量存储的基础技术,也是构建网络存储的基础技术.专家认为,磁盘阵列的性能优势得益于磁盘运行的并行性,提高设备运行并行度 ...
- BZOJ2721 [Violet 5]樱花
先令n! = a: 1 / x + 1 / y = 1 / a => x = y * a / (y - a) 再令 k = y - a: 于是x = a + a ^ 2 / k => ...
- PMP项目管理笔记 项目定义
项目的定义 项目是为创造独特的产品,服务或成果而进行临时性的工作. 项目是组织的经营需要与战略目标服务的. PMBOK 指南描述的项目管理知识,从本质上讲,是用来管理中等或以上规模,跨部门,跨专业的目 ...