1991.cpp (326B)
1 class Solution { 2 public: 3 int findMiddleIndex(vector<int> &nums) { 4 int left = 0; 5 int right = accumulate(nums.begin(), nums.end(), 0); 6 for (int i = 0; i < nums.size(); left += nums[i++]) { 7 right -= nums[i]; 8 if (left == right) return i; 9 } 10 return -1; 11 } 12 };