Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.

Example:

nums = [, , ]
target = The possible combination ways are:
(, , , )
(, , )
(, , )
(, )
(, , )
(, )
(, ) Note that different sequences are counted as different combinations. Therefore the output is .

Follow up:
What if negative numbers are allowed in the given array?
How does it change the problem?
What limitation we need to add to the question to allow negative numbers?

思路:递归求解。

为了避免相同的解重复计数,要将原数组中的重复数字剔除,这样子所有的情况都只会枚举一遍。

同时,为了提速,在递归过程中,可以用一个map记录子问题的结果,这样就可以节省时间。

补充:如果数组中有负数,则应该添加的额外条件是最多可以有几个数相加。

 class Solution {
public:
int help(vector<int>& nums, int target, unordered_map<int, int>& solutionCount) {
int count = ;
for (int i = ; i < nums.size() && nums[i] <= target; i++) {
if (nums[i] < target) {
int balance = target - nums[i];
if (solutionCount.count(balance))
count += solutionCount[balance];
else {
int subCount = help(nums, balance, solutionCount);
solutionCount.insert(make_pair(balance, subCount));
count += subCount;
}
}
else count++;
}
return count;
}
int combinationSum4(vector<int>& nums, int target) {
if (nums.size() == ) return ;
sort(nums.begin(), nums.end(), less<int>());
vector<int> distinctNum(, nums[]);
unordered_map<int, int> solutionCount;
for (int i = ; i < nums.size(); i++)
if (nums[i] != nums[i-]) distinctNum.push_back(nums[i]);
return help(distinctNum, target, solutionCount);
}
};

Combination Sum IV -- LeetCode的更多相关文章

  1. Combination Sum | & || & ||| & IV

    Combination Sum | Given a set of candidate numbers (C) and a target number (T), find all unique comb ...

  2. LC 377. Combination Sum IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  3. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  4. [LeetCode] 377. Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  5. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  6. Combination Sum II leetcode java

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

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

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

  8. Combination Sum III - LeetCode

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  9. 【LeetCode】377. Combination Sum IV 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. 遇到问题---java---myeclipse发布项目打包项目resource资源有缓存---log4j.properties新配置不起作用

    在使用myeclipse过程中遇到一个很奇怪的问题,无论是在myeclipse中deploy发布到tomcat或者打包打成war后在tomcat中运行解压,resource都有缓存的感觉. 比较明显的 ...

  2. Ubuntu下安装LNMP之nginx的安装

    Nginx 最初是作为一个 Web 服务器创建的,用于解决 C10k 的问题.作为一个 Web 服务器,它可以以惊人的速度为您的数据服务.但 Nginx 不仅仅是一个 Web 服务器,你还可以将其用作 ...

  3. C#语法糖大汇总【转发】

    首先需要声明的是“语法糖”这个词绝非贬义词,它可以给我带来方便,是一种便捷的写法,编译器会帮我们做转换:而且可以提高开发编码的效率,在性能上也不会带来损失.这让java开发人员羡慕不已,呵呵. 1.  ...

  4. 压缩跟踪Compressive Tracking

    好了,学习了解了稀疏感知的理论知识后,终于可以来学习<Real-Time Compressive Tracking>这个paper介绍的感知跟踪算法了.自己英文水平有限,理解难免出错,还望 ...

  5. Hadoop NameNode元数据相关文件目录解析

    在<Hadoop NameNode元数据相关文件目录解析>文章中提到NameNode的$dfs.namenode.name.dir/current/文件夹的几个文件: 1 current/ ...

  6. 初识 spl_autoload_register

    spl_autoload_register 一.首先我们看来自官网的定义 版本要求:php版本为5.1.2+ 说明:注册给定的函数作为__autoload的实现.即自动加载 函数参数说明: bool ...

  7. 汕头市队赛 SRM16 T2

    描述 猫和老鼠,看过吧?猫来了,老鼠要躲进洞里.在一条数轴上,一共有n个洞,位置分别在xi,能容纳vi只老鼠.一共有m只老鼠位置分别在Xi,要躲进洞里,问所有老鼠跑进洞里的距离总和最小是多少. 输入格 ...

  8. zlib库剖析(1):实现概览

    zlib库剖析(1):实现概览 http://blog.csdn.net/zhoudaxia/article/details/8034606 http://blog.chinaunix.net/uid ...

  9. float和double类型的存储方式

    Float double 类型在计算机的存储方式 计算机中只认识10的二进制数,那么该如何存储小数呢? 那么我们先看Floa类型: Float在计算机(32位)中是4个字节的,具体地:第一位为符号位0 ...

  10. HTML5 拖拽事件

    dragstart:拖拽开始 drag: 拖拽中,会不停的触发 dragend:拖拽结束 ondraglevea:有元素离开了本元素 ondragenter:有元素进入了本元素 ondragover: ...