C语言 | Leetcode C语言题解之第416题分割等和子集

题目:

题解:

cpp 复制代码
bool canPartition(int* nums, int numsSize) {
    if (numsSize < 2) {
        return false;
    }
    int sum = 0, maxNum = 0;
    for (int i = 0; i < numsSize; ++i) {
        sum += nums[i];
        maxNum = fmax(maxNum, nums[i]);
    }
    if (sum & 1) {
        return false;
    }
    int target = sum / 2;
    if (maxNum > target) {
        return false;
    }
    int dp[target + 1];
    memset(dp, 0, sizeof(dp));
    dp[0] = true;
    for (int i = 0; i < numsSize; i++) {
        int num = nums[i];
        for (int j = target; j >= num; --j) {
            dp[j] |= dp[j - num];
        }
    }
    return dp[target];
}
相关推荐
希望有朝一日能如愿以偿42 分钟前
力扣每日一题:统计梯形的数目
算法·leetcode·职场和发展
水饺编程2 小时前
第3章,[标签 Win32] :WM_CREATE 消息的产生
c语言·c++·windows·visual studio
会员果汁2 小时前
双向链式队列-C语言
c语言·数据结构
C语言不精2 小时前
c语言-优雅的多级菜单设计与实现
c语言·开发语言·算法
jyyyx的算法博客3 小时前
LeetCode 面试题 16.22. 兰顿蚂蚁
算法·leetcode
Q741_1473 小时前
C++ 高精度计算的讲解 模拟 力扣67.二进制求和 题解 每日一题
c++·算法·leetcode·高精度·模拟
leoufung3 小时前
LeetCode 98 Validate Binary Search Tree 深度解析
算法·leetcode·职场和发展
jyyyx的算法博客3 小时前
LeetCode 面试题 16.18. 模式匹配
算法·leetcode
star learning white4 小时前
xmC语言10
c语言·开发语言
ada7_4 小时前
LeetCode(python)——94.二叉
python·算法·leetcode·链表·职场和发展