当前位置: 首页 > article >正文

Java | Leetcode Java题解之第416题分割等和子集

题目:

题解:

class Solution {
    public boolean canPartition(int[] nums) {
        int n = nums.length;
        if (n < 2) {
            return false;
        }
        int sum = 0, maxNum = 0;
        for (int num : nums) {
            sum += num;
            maxNum = Math.max(maxNum, num);
        }
        if (sum % 2 != 0) {
            return false;
        }
        int target = sum / 2;
        if (maxNum > target) {
            return false;
        }
        boolean[] dp = new boolean[target + 1];
        dp[0] = true;
        for (int i = 0; i < n; i++) {
            int num = nums[i];
            for (int j = target; j >= num; --j) {
                dp[j] |= dp[j - num];
            }
        }
        return dp[target];
    }
}

http://www.kler.cn/news/315009.html

相关文章:

  • 国内可以使用的ChatGPT服务【9月持续更新】
  • 828华为云征文 | 云服务器Flexus X实例:多智能体对话框架 AutoGen 部署和实例运行
  • 重修设计模式-结构型-门面模式
  • python 实现binomial coefficient二项式系数算法
  • excel 单元格一直显示年月日
  • Contact Form 7最新5.9.8版错误修复方案
  • ClickHouse 与 Quickwit 集成实现高效查询
  • 适用于QF的存档系统
  • react的事件绑定
  • vulnhub(12):bob 1.0.1(gpg文件解密)
  • @PostConstruct
  • <刷题笔记> 力扣236题——二叉树的公共祖先
  • 全面详尽的 PHP 环境搭建教程
  • C++ 元编程
  • 18938 汉诺塔问题
  • 《深度学习》PyTorch 常用损失函数原理、用法解析
  • 【电力系统】基于遗传算法的33节点电力系统无功优化及MATLAB实现
  • LeetCode337. 打家劫舍III
  • springbootKPL比赛网上售票系统
  • Maven 项目无法下载某个依赖
  • 论 JAVA 集合框架中 接口与类的关系
  • 注册信息安全专业人员(CISP)和网络安全的联系与区别
  • FLStudio21Mac版flstudio v21.2.1.3430简体中文版下载(含Win/Mac)
  • windows cuda12.1 pytorch gpu环境配置
  • js之遍历方法
  • windows@文件系统链接@快捷方式@快捷键方式和符号链接及其对比
  • 本地提权【笔记总结】
  • 《AI:开启未来的无限可能》
  • 【django】局域网访问django启动的项目
  • MongoDB解说