力扣第1013题 将数组分成和相等的三部分
class Solution {
public:
bool canThreePartsEqualSum(vector<int>& A) {
int sum = accumulate(A.begin(), A.end(), 0);
if (sum % 3 != 0)
return false;
int temp = sum / 3;
int len = A.size();
int left = 0, right = len - 1;
int leftSum = A[left], rightSum = A[right];
while (left + 1 < right)
{
if (leftSum == temp && rightSum == temp)
{
return true;
}
if (leftSum != temp)
{
leftSum += A[++left];
}
if (rightSum != temp)
{
rightSum += A[--right];
}
}
return false;
}
};
原文地址:https://www.cnblogs.com/woodjay/p/12466315.html
时间: 2024-11-06 03:48:41