LeetCode(416):分割等和子集 Partition Equal Subset Sum(Java)

234 篇文章 1 订阅
177 篇文章 0 订阅

2019.10.21 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新)

github:https://github.com/ChopinXBP/LeetCode-Babel

这是一道经典的01背包问题,可以将题目看成在所有数字中不重复取若干个数字,能否使得最终和为数组和的一半。

建立dp数组,dp[i][j]代表从在数组前i个数字中不重复取数能否组成和为j的数。在数组前i个数字中不重复取数能否组成和为j的数,可能有两种情况:

1.数组的前i-1个数字就已经能够组合和为j的数,状态转移方程:

dp[i][j] = dp[i - 1][j];

2.加上第i个数字nums[i]正好使得条件达成,状态转移方程:

dp[i][j] = dp[i - 1][j - nums[i-1]];

根据dp数组的运算规律,可以将其从二维降至一维。


传送门:分割等和子集

Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.

Note:

Each of the array element will not exceed 100.

The array size will not exceed 200.

给定一个只包含正整数的非空数组。是否可以将这个数组分割成两个子集,使得两个子集的元素和相等。

注意:

每个数组中的元素不会超过 100

数组的大小不会超过 200

示例 1:
输入: [1, 5, 11, 5]
输出: true
解释: 数组可以分割成 [1, 5, 5] 和 [11].

示例 2:
输入: [1, 2, 3, 5]
输出: false
解释: 数组不能分割成两个元素和相等的子集.


import java.util.Arrays;

/**
 *
 * Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.
 * Note:Each of the array element will not exceed 100. The array size will not exceed 200.
 * 给定一个只包含正整数的非空数组。是否可以将这个数组分割成两个子集,使得两个子集的元素和相等。
 * 注意: 每个数组中的元素不会超过 100. 数组的大小不会超过 200
 *
 */

public class PartitionEqualSubsetSum {
    //动态规划:01背包
    public boolean canPartition(int[] nums) {
        int sum = 0;
        for(int num : nums){
            sum += num;
        }
        if((sum & 1) == 1){
            return false;
        }

        //dp[i][j]代表从在数组前i个数字中不重复取数能否组成和为j的数
        int target = sum >> 1;
        boolean[][] dp = new boolean[nums.length + 1][target + 1];
        dp[0][0] = true;

        for(int i = 1; i <= nums.length; i++){
            for(int j = target; j >= 0; j--){
                //在数组前i个数字中不重复取数能否组成和为j的数,可能有两种情况:
                //1.数组的前i-1个数字就已经能够组合和为j的数,状态转移方程:dp[i][j] = dp[i - 1][j];
                //2.加上第i个数字nums[i]正好使得条件达成,状态转移方程:dp[i][j] = dp[i - 1][j - nums[i-1]];
                dp[i][j] = dp[i - 1][j] || (j >= nums[i - 1] && dp[i - 1][j - nums[i - 1]]);
            }
        }

        return dp[nums.length][target];
    }

    //一维动态规划
    public boolean canPartition2(int[] nums) {
        int sum = 0;
        for(int num : nums){
            sum += num;
        }
        if((sum & 1) == 1){
            return false;
        }

        int target = sum >> 1;
        boolean[] dp = new boolean[target + 1];
        dp[0] = true;

        for(int num : nums){
            for(int j = target; j >= num; j--){
                dp[j] = dp[j] || dp[j - num];
            }
        }

        return dp[target];
    }
}




#Coding一小时,Copying一秒钟。留个言点个赞呗,谢谢你#

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值