Solution sharing - tabulation java 6ms


  • 0
    M
    class Solution {
        public int combinationSum4(int[] nums, int target) {
            if(nums == null || nums.length < 1){
                return 0;
            }
            int[] dp = new int[target+1];
            java.util.Arrays.fill(dp, 0);
            dp[0] = 1;
            for(int i=1;i<dp.length;i++){
                for(int j=0;j<nums.length;j++){
                    if(nums[j] <= i){
                        dp[i] += dp[i-nums[j]];
                    }
                }
            }
            return dp[target];
        }
    }
    

Log in to reply
 

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