C++ very simple 6-line O(n) time, O(1) space solution


  • 0
    M
        int pivotIndex(vector<int>& nums) {
            int sum=0, pre=0;
            for(int i=0;i<nums.size();i++) sum+=nums[i];
            for(int i=0;i<nums.size();i++) {
                if(sum-pre-nums[i]==pre) return i;
                pre+=nums[i];
            }
            return -1;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.