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一个接口可以继承另外一个接口吗

    一个接口可以继承多个接口. interface C extends A, B {}是可以的. 一个类可以实现多个接口: class D implements A,B,C{} 但是一个类只能继承一个类, ...

  2. Any gotchas at all with converting from MyISAM to InnoDB?

    Q: I'm ready to move from MyISAM to InnoDB but wanted to know if there was a full list of things to ...

  3. HDU3605:Escape(状态压缩+最大流)

    Escape Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  4. poj 2378 Tree Cutting 树形dp

    After Farmer John realized that Bessie had installed a "tree-shaped" network among his N ( ...

  5. DOM 2

    1.对文档的信息进行检索常用的方法: getElementById; getElementsByTagName; getAttribute;//得到的是属性值 2把需要的信息添加到DOM中常用的方法: ...

  6. eclipse配置文件内存设置

    1.-Xms64m -Xmx128m 2.配置文件的修改 http://wenku.baidu.com/link?url=spM-qCe0qHdhiykzwuzp-vBtcQrVtAzYiWe8uex ...

  7. centos7.6升级ssh7.9、安装PHP7.2、Nginx1.15.9、PHP加密扩展php_screw1.5

    1.centos7 安装PHP7.2版本 #查询是否安装过php yum list installed | grep php yum provides php #移除php yum remove ph ...

  8. 斯特林数(Stirling number)

    在组合数学,Stirling 数可指两类数,第一类Stirling 数和第二类 Stirling 数,都是由18世纪数学家 James Stirling 提出的. Stirling 数有两种,第一类和 ...

  9. phpAdmin 修改密码后拒绝登陆

    phpMyadmin没配置正确,打开 phpMyadmin 目录找到config.inc.php文件,查找到$cfg['Servers'][$i]['password']='';这行,在''中输入你正 ...

  10. 【LuoguP1169 bzoj1057】[ZJOI2007]棋盘制作

    首先把矩阵转化一下,把横纵坐标和为偶数点的值取反,这样就转化成求最大的'0'或'1'矩阵. 这道题每个数字是在格子内的,不能在边界包含障碍点. 求最大的0矩阵时,把1作为障碍点.求1同理. 然后求最接 ...