描述
求和为目标值的组合
解决
DP
class Solution {
public:
int combinationSum4(vector<int>& nums, int target) {
arr = new int[target + 1];
memset(arr, -1, sizeof(int) * (target + 1));
arr[0] = 1;
return func(nums, target);
}
int func(vector<int>& nums, int target)
{
if (arr[target] != -1)
return arr[target];
int zz = 0;
for (int i = 0; i < nums.size(); ++i)
{
if (target >= nums[i])
{
zz += func(nums, target - nums[i]);
}
}
arr[target] = zz;
return arr[target];
}
int *arr;
};