377. Combination Sum IV | Leetcode Dynamic Programming
来源:互联网 发布:网络玄幻小说典藏版 编辑:程序博客网 时间:2024/06/15 15:33
Description
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:
Thinking
用动态规划解决这个问题。用nums中的数字加出target,求共有多少种组合方式。设置一个size为target+1的vector tar用于记录加法总数,假设target为0,则只有一种,不用任何nums中的数字,即tar[0]=1。
求tar[i]时,遍历nums数组,若tar[i]大于nums[j],则增加tar[i-nums[j]]种方法。状态转移方程如下:
tar[i] += tar[i - nums[j]];
Solution
class Solution {public: int combinationSum4(vector<int>& nums, int target) { vector<int> tar(target + 1, 0); tar[0] = 1; for(int i = 1; i <= target; i++){ for(int j = 0; j < nums.size(); j++){ int num = nums[j]; if(i - nums[j] >= 0){ tar[i] += tar[i - num]; } } } return tar[target]; }};
阅读全文
0 0
- 377. Combination Sum IV | Leetcode Dynamic Programming
- (M)Dynamic Programming:377. Combination Sum IV
- week9- Dynamic Programming-NO.377. Combination Sum IV
- Leetcode 377. Combination Sum IV
- 【LeetCode】377. Combination Sum IV
- [leetcode] 377. Combination Sum IV
- 【leetcode】377. Combination Sum IV
- LeetCode:377. Combination Sum IV
- [leetcode] 377.Combination Sum IV
- 377. [LeetCode]Combination Sum IV
- leetcode-377. Combination Sum IV
- LeetCode 377. Combination Sum IV
- Leetcode 377. Combination Sum IV
- 【LeetCode】 377. Combination Sum IV
- [LeetCode]377. Combination Sum IV
- [leetcode]377. Combination Sum IV
- Leetcode 377. Combination Sum IV
- leetcode 377. Combination Sum IV
- 回环变位(2017校招一模-----循环单词)
- 135. Candy
- Android StudioDebug调试详细大全
- 50多条实用mysql数据库优化建议
- 1.2 多个线程多个锁
- 377. Combination Sum IV | Leetcode Dynamic Programming
- leetcode 44. Wildcard Matching
- 限时独占2个月:曝OPPO R11前期独家使用骁龙660
- 输入输出流 (文件读写)
- leetcode-java.T013_RomarToInteger 罗马数字转成整数数字
- SUDT 3926 bLue的二叉树 [KMP or hash]【思维】
- 简单的在线留言系统
- leetcode-java.T012_IntegerToRoman 数字转罗马数字
- HackerRank