美文网首页
动态十二:组合总和 Ⅳ

动态十二:组合总和 Ⅳ

作者: 程一刀 | 来源:发表于2021-09-01 19:32 被阅读0次

题目地址: https://leetcode-cn.com/problems/combination-sum-iv/
题目描述: 给定一个由正整数组成且不存在重复数字的数组,找出和为给定目标正整数的组合的个数。
示例:
nums = [1, 2, 3] target = 4
所有可能的组合为: (1, 1, 1, 1) (1, 1, 2) (1, 2, 1) (1, 3) (2, 1, 1) (2, 2) (3, 1)
请注意,顺序不同的序列被视作不同的组合。
因此输出为 7。

参考代码:

class Solution {
public:
    int combinationSum4(vector<int>& nums, int target) { // 1 2 3 -》4

        vector<int> dp = vector<int>(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]  < INT_MAX - dp[j-nums[i]]) {
                    dp[j] = dp[j] + dp[j-nums[i]];
                }
            }
        }
        
        return dp[target];
    }
};

参考链接: https://github.com/youngyangyang04/leetcode-master/blob/master/problems/0377.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8C%E2%85%A3.md

相关文章

  • 动态十二:组合总和 Ⅳ

    题目地址: https://leetcode-cn.com/problems/combination-sum-i...

  • 组合总和

    给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可...

  • 组合总和

  • 组合总和

    Algorithm 39. Combination Sum[https://leetcode.com/proble...

  • LeetCode:组合总和

    组合总和 题目叙述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 cand...

  • 2018-08-13 LeetCode回溯算法(组合总和)总结

    组合总和candidates 中的数字可以无限制重复被选取 组合总和 IIcandidates 中的每个数字在每个...

  • 39. 组合总和

    39. 组合总和 很慢的dfs

  • 39. 组合总和

    给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可...

  • leetcode 39 组合总和

    今天很奇怪,我用的是dfs,AC代码是 但是前面一直用下面的代码,过不了 后来发现我这样写会改变sum的值,下次要...

  • 39.组合总和

    题目给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所...

网友评论

      本文标题:动态十二:组合总和 Ⅳ

      本文链接:https://www.haomeiwen.com/subject/kwwziltx.html