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

posted on 2017-06-09 09:18  Sempron2800+  阅读(121)  评论(0编辑  收藏  举报