欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > 代码随想录算法训练营第34天

代码随想录算法训练营第34天

2025/6/4 10:50:17 来源:https://blog.csdn.net/m0_62262158/article/details/140363467  浏览:    关键词:代码随想录算法训练营第34天

LeetCode 416. 分割等和子集

链接

class Solution {
public:bool canPartition(vector<int>& nums) {vector<int> dp(10001, 0);int sum = accumulate(nums.begin(), nums.end(), 0);if(sum % 2 == 1) {return false;}int target = sum / 2;for(int i = 0; i < nums.size(); i++) {for(int j = target; j >= nums[i]; j--) {dp[j] = max(dp[j], dp[j - nums[i]] + nums[i]);}}if(dp[target] == target) {return true;}return false;}
};

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词