From 0a9653df8457535038888a57e05ba873109eccce Mon Sep 17 00:00:00 2001 From: Yukun J Date: Wed, 24 Aug 2022 11:22:43 -0400 Subject: [PATCH] =?UTF-8?q?Update:=200416=20=E4=BD=BF=E7=94=A8STL=E6=B1=82?= =?UTF-8?q?=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0416.分割等和子集.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index 03eae8ef..9c90ea27 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.md @@ -143,15 +143,12 @@ dp[j]的数值一定是小于等于j的。 class Solution { public: bool canPartition(vector& nums) { - int sum = 0; - + // 使用标准库函数 便捷求和 + int sum = accumulate(nums.begin(), nums.end(), 0); // dp[i]中的i表示背包内总和 // 题目中说:每个数组中的元素不会超过 100,数组的大小不会超过 200 // 总和不会大于20000,背包最大只需要其中一半,所以10001大小就可以了 vector dp(10001, 0); - for (int i = 0; i < nums.size(); i++) { - sum += nums[i]; - } if (sum % 2 == 1) return false; int target = sum / 2;