public class Solution { private int[] dp; public int CombinationSum4(int[] nums, int target) { dp = new int[target + 1]; for (int i = 0; i < dp.Length; i++) { dp[i] = -1; } dp[0] = 1; return helper(nums, target); } private int helper(int[] nums, int target) { if (dp[target] != -1) { return dp[target]; } int res = 0; for (int i = 0; i < nums.Length; i++) { if (target >= nums[i]) { res += helper(nums, target - nums[i]); } } dp[target] = res; return res; } }
https://leetcode.com/problems/combination-sum-iv/#/description