这两道题的基本思路和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. linux笔记:linux帮助命令,man,help,whatis,apropos

    命令名称:man功能:获得帮助信息命令所在路径:/usr/bin/man用法:man 命令或配置文件其他:会调用less来查看该命令或配置文件的帮助信息. 命令名称:whatis功能:获得命令的简短介 ...

  2. Java substring() 方法

    Java String类 substring() 方法返回字符串的子字符串. 语法 public String substring(int beginIndex) 或 public String su ...

  3. swf2pdf转swf时字符集问题【转】

    今天转了一个的pdf是出现字符集问题,并转换的swf为乱码.出现的错误如下. 错误的原因是缺少中文字符集GBK-EUC-H.解决方法使用xpdf增加缺少的字符集.解决步骤如下: (一) 下载相关的xp ...

  4. discuz核心类库class_core的函数注释

    class discuz_core { // 数据库存储引擎 var $db = null; // 内存缓冲object var $mem = null; // 会话 object var $sess ...

  5. 如何使用java调用DLL运行C++(初篇)

    JNI:Java Native Interface,简称JNI,是Java平台的一部分,可用于让Java和其他语言编写的代码进行交互. 下面是从网上摘取的JNI工作示意图:

  6. hduoj-----(1068)Girls and Boys(二分匹配)

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. 正则表达式 (C++)

    本主题讨论各正则表达式引擎的语法. 正则表达式语法 语法摘要 语义详细信息 匹配和搜索 格式标志 正则表达式语法   元素  元素可以是下列项之一: 一般字符,可匹配目标序列中的相同字符. 通配符“. ...

  8. IO流--字符流

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java ...

  9. 20145236 《Java程序设计》 第十周学习总结

    20145236 <Java程序设计> 第十周学习总结 Java网络编程 Java网络编程技术 Java语言是在网络环境下诞生的,所以Java语言虽然不能说是对于网络编程的支持最好的语言, ...

  10. Spring使用RowMapper将数据中的每一行封装成用户定义的类

    1.dao public interface MapperSelecteAllEmpDao { public List<Emp> all(); } 2.实现类 public class M ...