Backpack VI
描述
笔记
数据
评测
Given an integer array nums with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.
注意事项
The different sequences are counted as different combinations.
您在真实的面试中是否遇到过这个题? Yes
样例
Given nums = [1, 2, 4], target = 4
The possible combination ways are:
[1, 1, 1, 1]
[1, 1, 2]
[1, 2, 1]
[2, 1, 1]
[2, 2]
[4]
return 6
标签
动态规划
相关题目
思路:动态规划
int dp[target+1] ;dp[0]=1;
for(int i=1;i<target+1;++i){
for(auto a:nums){
if(i>=a)
dp[i]+=dp[i-a];
}
}
return dp[target];
这是最初代码,思路是正确,但是一直报错,同一组数据,多次测试,答案居然不断翻倍