Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0a9653d

Browse files
committed
Update: 0416 使用STL求和
1 parent fbe1e00 commit 0a9653d

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

‎problems/0416.分割等和子集.md‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,12 @@ dp[j]的数值一定是小于等于j的。
143143
class Solution {
144144
public:
145145
bool canPartition(vector<int>& nums) {
146-
int sum = 0;
147-
146+
// 使用标准库函数 便捷求和
147+
int sum = accumulate(nums.begin(), nums.end(), 0);
148148
// dp[i]中的i表示背包内总和
149149
// 题目中说:每个数组中的元素不会超过 100,数组的大小不会超过 200
150150
// 总和不会大于20000,背包最大只需要其中一半,所以10001大小就可以了
151151
vector<int> dp(10001, 0);
152-
for (int i = 0; i < nums.size(); i++) {
153-
sum += nums[i];
154-
}
155152
if (sum % 2 == 1) return false;
156153
int target = sum / 2;
157154

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /