Combination Sum IV -- LeetCode
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的更多相关文章
- Combination Sum | & || & ||| & IV
Combination Sum | Given a set of candidate numbers (C) and a target number (T), find all unique comb ...
- LC 377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- Combination Sum II leetcode java
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV
▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
- Combination Sum III - LeetCode
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 【LeetCode】377. Combination Sum IV 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- string 类型转换
string转int "; int n = atoi(str.c_str()); cout << n << endl; int转string #include < ...
- 什么是node.js的事件驱动编程
Node.js现在非常活跃,相关生态社区已经超过Lua(基本上比较知名的功能都有nodejs模块实现).但是我们为何要使用Node.Js?相比传统的webserver服务模式,nodejs有什么优点优 ...
- How do I see what character set a database / table / column is in MySQL?
Q: How do I see what the character set that a MySQL database, table and column are in? Is there some ...
- webpack3基础知识
## 本地化安装webpack ## 1. npm init //npm初始化生成package.json文件 2. npm install --save-dev webpack //安装webpac ...
- codechef September Challenge 2017 Sereja and Commands
———————————————————————————— 这道题维护一下原序列的差分以及操作的差分就可以了 记得倒着差分操作 因为题目保证操作2的l r 小与当前位置 #include<cstd ...
- [POJ2068]Nim解题报告
Let's play a traditional game Nim. You and I are seated across a table and we have a hundred stones ...
- winds dlib人脸检测与识别库
在人脸检测与人脸识别库中dlib库所谓是非常好的了.检测效果非常ok,下面我们来了解一下这个神奇的库吧! 第一步我们首先学会安装:dlib ,winds+pytho3.6.5 Windows不支持p ...
- keras_实现cnn_手写数字识别
# conding:utf-8 import os os.environ[' import numpy as np from keras.models import Sequential from k ...
- bzoj 1601 最小生成树
原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=1601 最小生成树的比较水的题,我们只需要加一个源点,连向所有的点,边权为每个点建水库的代价 ...
- 多表查询与pymysql
一.子查询 #1:子查询是将一个查询语句嵌套在另一个查询语句中. #2:内层查询语句的查询结果,可以为外层查询语句提供查询条件. #3:子查询中可以包含:IN.NOT IN.ANY.ALL.EXIST ...