simple java dp solution, easy to understand


  • 0
    Z
        public int combinationSum4(int[] nums, int target) {
            int[] res = new int[target+1];
            res[0] = 1;
            for(int cur=1; cur<=target; cur++) {
                int total = 0;
                for(int i=0; i<nums.length; i++) {
                    if(cur - nums[i] >= 0) {
                        total += res[cur-nums[i]];
                    }
                }
                res[cur] = total;
            }
            
            return res[target];
        }
    

Log in to reply
 

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