C# Solution.


  • 0
    J

    C# Solution:

    Straight forward solution:

    public int PivotIndex(int[] nums)
        {
            if (nums == null || nums.Length == 0)
                return -1;
    
            int sum = nums.Sum();
            int leftsum = 0;
    
            for (int i = 0; i < nums.Length; i++)
            {
                if ((sum = (sum - nums[i])) == leftsum)
                    return i;
                else
                    leftsum += nums[i];
            }
    
            return -1;
        }

Log in to reply
 

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