每日一题 416 分割等和子集(01背包)

题目

分割等和子集

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

示例 1:

输入:nums = [1,5,11,5]

输出:true

解释:数组可以分割成 [1, 5, 5] 和 [11] 。

示例 2:

输入:nums = [1,2,3,5]

输出:false

解释:数组不能分割成两个元素和相等的子集。

提示:

1 <= nums.length <= 200

1 <= nums[i] <= 100

题解

记忆化搜索

java 复制代码
class Solution {
    private int[] nums;
    //这里如果定义布尔数组的话将会无法存储已经遍历的路径
    private int[][] cache;

    public boolean canPartition(int[] nums) {
        int target = 0;
        for (int x : nums) {
            target += x;
        }
        if (target % 2 != 0 || target < 0) {
            return false;
        }
        target /= 2;
        this.nums = nums;
        int n = nums.length;
        cache = new int[n][target + 1];
        for (int i = 0; i < n; i++) {
            Arrays.fill(cache[i],-1);
        }
        return dfs(n - 1, target);
    }

    public boolean dfs (int i, int c) {
        if (i < 0) {
            return c == 0;
        }
        if (cache[i][c] != -1) {
            return cache[i][c] > 0 ? true : false;
        }
        if (c < nums[i]) {
            cache[i][c] = dfs(i - 1, c) ? 1 : 0;
            return dfs(i - 1, c);
        }
        cache[i][c] = (dfs(i - 1, c) || dfs(i - 1, c - nums[i])) ? 1 : 0; 
        return dfs(i - 1, c) || dfs(i - 1, c - nums[i]);
    }
}

1:1递推

两个数组空间优化

java 复制代码
class Solution {
    public boolean canPartition(int[] nums) {
        int target = 0;
        for (int x : nums) {
            target += x;
        }
        if (target % 2 != 0 || target < 0) {
            return false;
        }
        target /= 2;
        int n = nums.length;
        boolean[][] f = new boolean[2][target + 1];
        f[0][0] = true;
        for (int i = 0; i < n; i++) {
            for (int c = 0; c <= target; c++) {
                if (c < nums[i]) {
                    f[(i + 1) % 2][c] = f[i % 2][c];
                } else {
                    f[(i + 1) % 2][c] = f[i % 2][c] || f[i % 2][c - nums[i]];
                }
            }
        }
        return f[n % 2][target];
    }
}

一个数组空间优化

java 复制代码
class Solution {
    public boolean canPartition(int[] nums) {
        int target = 0;
        for (int x : nums) {
            target += x;
        }
        if (target % 2 != 0 || target < 0) {
            return false;
        }
        target /= 2;
        int n = nums.length;
        boolean[] f = new boolean[target + 1];
        f[0] = true;
        for (int x : nums) {
            for (int c = target; c >= x; c--) {
                f[c] = f[c] || f[c - x];
            }
        }
        return f[target];
    }
}
相关推荐
黎雁·泠崖4 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
代码游侠4 小时前
C语言核心概念复习——网络协议与TCP/IP
linux·运维·服务器·网络·算法
2301_763472465 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
qq_12498707535 小时前
基于SSM的动物保护系统的设计与实现(源码+论文+部署+安装)
java·数据库·spring boot·毕业设计·ssm·计算机毕业设计
Coder_Boy_5 小时前
基于SpringAI的在线考试系统-考试系统开发流程案例
java·数据库·人工智能·spring boot·后端
Mr_sun.5 小时前
Day06——权限认证-项目集成
java
瑶山5 小时前
Spring Cloud微服务搭建四、集成RocketMQ消息队列
java·spring cloud·微服务·rocketmq·dashboard
abluckyboy5 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法
2301_818732065 小时前
前端调用控制层接口,进不去,报错415,类型不匹配
java·spring boot·spring·tomcat·intellij-idea
园小异5 小时前
2026年技术面试完全指南:从算法到系统设计的实战突破
算法·面试·职场和发展