美文网首页
416. Partition Equal Subset Sum

416. Partition Equal Subset Sum

作者: becauseyou_90cd | 来源:发表于2018-07-25 06:13 被阅读0次

    解题思路:

    1. 用双循环去更新dp[j]:dp[j] = dp[j] || dp[j - nums[i]]

    代码:

    class Solution {
    public boolean canPartition(int[] nums) {

        int len = nums.length;
        int sum = 0;
        for(int i = 0; i < len; i++){
            sum += nums[i];
        }
        if(sum % 2 != 0) return false; 
        int target = sum/2;
        boolean[] dp = new boolean[target + 1];
        dp[0] = true;
        for(int i = 1; i <= len; i++){
            for(int j = target; j >= nums[i - 1]; j--){
                dp[j] = dp[j] || dp[j - nums[i - 1]];
            }
        }
        return dp[target];
    }
    

    }

    相关文章

      网友评论

          本文标题:416. Partition Equal Subset Sum

          本文链接:https://www.haomeiwen.com/subject/hudemftx.html