题目描述
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] |
一开始以为是一道简单的dfs的题目,结果暴力做直接TLE。
这题应该用DP来做,令dp[i]为这个序列和为i的排列个数。状态转移方程为:
意思就是$j$是nums中的数字,这个dp的规律就是对于目标和target,它是所有和为target-j的可能性之和,相当于在所有target-j的目标最后加上了j作为一个新的排列。
代码实现
1 | class Solution { |