share my java solutiom, beating 88%!


  • 0
    T
    public class Solution {
        public int combinationSum4(int[] nums, int target) {
              if(target<=0) return 0;
              if(nums==null||nums.length<1) return 0;
              Integer[] dp=new Integer[target+1];
              dp[target]=1;
              
              int sum=countcombinations(nums,target,dp,0);
              return sum;
        }
        
        public int countcombinations(int[] nums,int target,Integer[] dp,int cursum){
               if(dp[cursum]!=null) return dp[cursum];
               
               int sum=0;
               for(int i=0;i<nums.length;i++){
                   if(cursum+nums[i]<=target){
                    
                       int t=countcombinations(nums,target,dp,cursum+nums[i]);
                       sum+=t;
                   }
               }
               
               dp[cursum]=sum;
               return sum;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.