思路
有关子数组和的问题可以思考能否用前缀和方法解决
class Solution {
public:
int pivotIndex(vector<int>& nums) {
int sum = 0;
for (int& n : nums) {
sum += n;
}
int preSum = 0;
for (int i = 0; i < nums.size(); ++i) {
if (2 * preSum + nums[i] == sum) return i;
else preSum += nums[i];
}
return -1;
}
};