C语言 | Leetcode C语言题解之第377题组合总和IV

题目:

C语言 | Leetcode C语言题解之第377题组合总和IV_第1张图片

题解:

int combinationSum4(int* nums, int numsSize, int target) {
    int dp[target + 1];
    memset(dp, 0, sizeof(dp));
    dp[0] = 1;
    for (int i = 1; i <= target; i++) {
        for (int j = 0; j < numsSize; j++) {
            if (nums[j] <= i && dp[i - nums[j]] < INT_MAX - dp[i]) {
                dp[i] += dp[i - nums[j]];
            }
        }
    }
    return dp[target];
}

你可能感兴趣的:(分享,C语言,Leetcode,题解)