Simple Swift Solution


  • 0
    R
    class Solution {
        func pivotIndex(_ nums: [Int]) -> Int {
            var right = 0
            for i in 0..<nums.count {
                right += nums[i]
            }
            
            var left = 0
            
            for i in 0..<nums.count {
                right -= nums[i]
                if left == right {
                    return i
                }
                left += nums[i]
            }
            return -1
            
        }
    }
    

Log in to reply
 

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