Simple Java DP


  • 2
    M
    public class Solution {
        public int combinationSum4(int[] nums, int target) {
            int[] ways = new int[target + 1];
            ways[0] = 1;
            for (int i = 1; i <= target; i++) {
                for (int j = 0; j < nums.length; j++) {
                    if (i - nums[j] >= 0) {
                        ways[i] += ways[i - nums[j]];
                    }
                }
            }
            return ways[target];
        }
    }

Log in to reply
 

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