377. Combination Sum IV
来源:互联网 发布:淘宝兼职都干什么 编辑:程序博客网 时间:2024/05/19 17:58
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.
- 解题思路:
- dp[i]:表示i由数组nums组成的数目,我们可以观察到 i - nums[j] + nums[j] = i;
- 则有递推公式:dp[i] = sum(dp[i-nums[j]) , j =0 .. length-1;因此的除了以下的解法:
class Solution {public: int combinationSum4(vector<int>& nums, int target) { int MAX = target+1; vector<int> dp(target+1,0); dp[0] = 1; for(int i = 1;i <= target;++i){ for(int j = 0;j < nums.size();++j){ if(i>=nums[j]&&dp[i-nums[j]]>0){ dp[i] += dp[i-nums[j]]; } } //printf("dp[%d] = %d\n\r",i,dp[i]); } return dp[target]; }};
阅读全文
0 0
- 377. Combination Sum IV
- 377. Combination Sum IV
- 377. Combination Sum IV
- 377. Combination Sum IV
- 377. Combination Sum IV
- 377. Combination Sum IV
- 377. Combination Sum IV
- 377. Combination Sum IV
- 377. Combination Sum IV
- 377. Combination Sum IV
- 377. Combination Sum IV
- 377. Combination Sum IV
- 377. Combination Sum IV**
- 377. Combination Sum IV
- 377. Combination Sum IV
- 377. Combination Sum IV
- 377. Combination Sum IV
- 377. Combination Sum IV
- POJ 2942:Knights of the Round Table tarjan点双联通分量 二分图染色找奇环
- 派生类构造函数和析构函数的构造规则
- 561. Array Partition I (数组分组)
- Android中AsyncTask的使用
- 数字和表达式
- 377. Combination Sum IV
- 7. Reverse Integer (翻转一个整数)
- House Robber II---lintcode
- 数据结构与算法 —— 二分查找的递归方式
- MySQL-各个约束之间的区别
- 利用mnist数据集的demo来做识别单张图片数字
- WPF委托的运用,多线程 自定义事件
- 461. Hamming Distance(汉明距离)
- 新装的Ubuntu(linux)不能上网怎么办