导航

377. Combination Sum IV

Posted on 2016-07-29 17:44  骄阳照林  阅读(139)  评论(0编辑  收藏  举报

 

 

 

思想: dp

状态转移方程:dp[i]=Σdp[i-nums[k]]  0<=k<=nums.length 

代码: 

public class Solution {
    public int combinationSum4(int[] nums, int target) {
        int len = nums.length;
        int[]dp = new int[target+1];
        dp[0]=1;
        Arrays.sort(nums);
        for(int i =0;i<=target;i++){
            for(int num: nums){
                if(i<num)
                    break;
                dp[i] = dp[i]+dp[i-num];    
            }
        }
        return dp[target];
    }
}