Skip to content

377. 组合总和 Ⅳ: 初始化dp[0] = 0 #553

Open
@fuqianz

Description

@fuqianz

class Solution {
public:
int combinationSum4(vector& nums, int target) {
vector dp(target+1, 0);

    for (int t = 0; t < target + 1; ++ t) {
        for (int x : nums) {
            
            if (t >=x && dp[t]  <= INT_MAX - dp[t-x]) {
            
                if (t > x) dp[t] += dp[t-x];

                if (t == x && dp[t]  <= INT_MAX - 1) {
                    dp[t] += 1; //当前这个数字可以组成一个集合 满足target
                }
            }
        }
    }
    return dp[target];
}

};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions