C++ Concise code O(1) Space


  • 0
    X
    class Solution {
    public:
        int pivotIndex(vector<int>& nums) {
            partial_sum(nums.cbegin(), nums.cend(), nums.begin());
            if (!nums.empty() && nums.back() - nums.front() == 0) return 0;
            for (int i = 1; i < nums.size(); ++i)
                if (nums[i - 1] == nums.back() - nums[i]) return i;
            return -1;
        }
    };
    

Log in to reply
 

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