
class Solution {
public:
int pivotIndex(vector<int> &nums)
{
int total = accumulate(nums.begin(), nums.end(), 0);
int sum = 0;
for (int i = 0; i < nums.size(); ++i)
{
if (2 * sum + nums[i] == total)
{
return i;
}
sum += nums[i];
}
return -1;
}
};
本文介绍了一种名为Solution的类,其pivotIndex方法用于在一个旋转数组中查找使得2*元素和等于数组总和的特定元素位置。通过遍历并调整元素累加和,该算法有效地定位了符合条件的索引。
530

被折叠的 条评论
为什么被折叠?



