思路
- dp数组定义:总和为j 共有dp[j]种组合
- 递推公式:
dp[j] += dp[j - nums[i]];
- dp数组初始化:
dp[0] = 1
- 遍历顺序:求排列问题,先背包后物品
- 时间复杂度:
代码
class Solution {
public:int combinationSum4(vector<int>& nums, int target) {vector<unsigned long long> dp(target + 1, 0);dp[0] = 1;for (int j = 0; j <= target; j++) {for (int i = 0; i < nums.size(); i++) {if (j >= nums[i]) {dp[j] += dp[j - nums[i]];}}}return dp[target];}
};