题目地址
LeetCode#377 Combination Sum IV
题目描述
Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.
Example:
1 | nums = [1, 2, 3] |
Follow up:
What if negative numbers are allowed in the given array?
How does it change the problem?
What limitation we need to add to the question to allow negative numbers?
解题思路
看到题后下意识的用了递归,然后就超时了。。。。
这道题的解法应该是 DP ,递归公式则为
dp[i] = {sum(dp[i-x1] , dp[i-x2] , dp[i-x3] ……) | x in nums && nums[x] < i}
解题代码【.CPP】
1 | class Solution { |