题解 | #加起来和为目标值的组合(四)#
加起来和为目标值的组合(四)
https://www.nowcoder.com/practice/7a64b6a6cf2e4e88a0a73af0a967a82b?tpId=117&tqId=39470&rp=1&ru=/exam/oj&qru=/exam/oj&sourceUrl=%2Fexam%2Foj%3Fpage%3D4%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D117&difficulty=undefined&judgeStatus=undefined&tags=&title=
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @param target int整型 * @return int整型 */ int combination(vector<int>& nums, int target) { // write code here vector<int> dp(target + 1); dp[0] = 1; for (int i = 1; i <= target; ++i) { for (const auto &num : nums) { if (i - num >= 0) { dp[i] += dp[i - num]; } } } return dp[target]; } };