标签:public tar targe target 结果 == 存在 循环 tor
1 //递归+记忆化 2 class Solution 3 { 4 vector<int> memo; 5 public: 6 int combinationSum4(vector<int>& nums, int target) 7 { 8 //memo数组一般取target + 1 9 memo = vector<int>(target + 1,-1); 10 return DFS(nums,target); 11 } 12 13 int DFS(vector<int>& nums,int target) 14 { 15 //如果target减到0,结果加1 16 if(target == 0) return 1; 17 18 //如果当前和的值不为-1,则存在,直接返回即可 19 if(memo[target] != -1) return memo[target]; 20 21 int res = 0;//计数 22 for(auto a : nums) 23 { 24 //当前和必须大于等于当前数,则进行递归 25 if(target >= a) res += DFS(nums,target - a); 26 } 27 memo[target] = res;//循环做完之后保存res在记忆化数组里 28 return res; 29 } 30 };
标签:public tar targe target 结果 == 存在 循环 tor
原文地址:https://www.cnblogs.com/yuhong1103/p/12517471.html