Java DP Solution (needn't to sort)


  • 1
    E
    public class Solution {
        public int combinationSum4(int[] nums, int target) {
            int[] res = new int[target+1];
            res[0] = 1;
            for(int i=1; i<=target; i++){
                for(int x : nums){
                    if(i>=x) res[i] += res[i-x];
                }
            }
            return res[target];
        }
    }
    

Log in to reply
 

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