★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-keibatkv-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given an array of integers nums
, write a method that returns the "pivot" index of this array.git
We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.github
If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.數組
Example 1:微信
Input: nums = [1, 7, 3, 6, 5, 6] Output: 3 Explanation: The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3. Also, 3 is the first index where this occurs.
Example 2:app
Input: nums = [1, 2, 3] Output: -1 Explanation: There is no index that satisfies the conditions in the problem statement.
Note:this
nums
will be in the range [0, 10000]
.nums[i]
will be an integer in the range [-1000, 1000]
.給定一個整數類型的數組 nums
,請編寫一個可以返回數組「中心索引」的方法。spa
咱們是這樣定義數組中心索引的:數組中心索引的左側全部元素相加的和等於右側全部元素相加的和。code
若是數組不存在中心索引,那麼咱們應該返回 -1。若是數組有多箇中心索引,那麼咱們應該返回最靠近左邊的那一個。htm
示例 1:
輸入: nums = [1, 7, 3, 6, 5, 6] 輸出: 3 解釋: 索引3 (nums[3] = 6) 的左側數之和(1 + 7 + 3 = 11),與右側數之和(5 + 6 = 11)相等。 同時, 3 也是第一個符合要求的中心索引。
示例 2:
輸入: nums = [1, 2, 3] 輸出: -1 解釋: 數組中不存在知足此條件的中心索引。
說明:
nums
的長度範圍爲 [0, 10000]
。nums[i]
將會是一個範圍在 [-1000, 1000]
的整數。140ms
1 class Solution { 2 func pivotIndex(_ nums: [Int]) -> Int { 3 if nums.count < 3 { 4 return -1 5 } 6 7 var sum = 0 8 for i in nums { 9 sum += i 10 } 11 12 var left = 0 13 var right = 0 14 15 for i in 0..<nums.count { 16 17 left = left + nums[i] 18 right = sum - left 19 20 if left - nums[i] == right { 21 return i 22 } 23 } 24 return -1 25 } 26 }
1 class Solution { 2 func pivotIndex(_ nums: [Int]) -> Int { 3 var sum:Int = nums.reduce(0,+) 4 var curSum:Int = 0 5 var n:Int = nums.count 6 for i in 0..<n 7 { 8 if sum - nums[i] == 2 * curSum 9 { 10 return i 11 } 12 curSum += nums[i] 13 } 14 return -1 15 } 16 }
156ms
1 class Solution { 2 func pivotIndex(_ nums: [Int]) -> Int { 3 guard nums.count > 0 else { 4 return -1 5 } 6 7 var leftSums: [Int] = [0] 8 var rightSums: [Int] = [0] 9 10 var sum: Int = nums.first! 11 for i in 1..<nums.count { 12 leftSums.append(sum) 13 let num = nums[i] 14 sum += num 15 } 16 17 sum = nums.last! 18 for i in (0..<nums.count - 1).reversed() { 19 rightSums.append(sum) 20 sum += nums[i] 21 } 22 23 rightSums = rightSums.reversed() 24 25 for i in 0..<nums.count { 26 if leftSums[i] == rightSums[i] { 27 return i 28 } 29 } 30 31 return -1 32 } 33 }
160ms
1 class Solution { 2 func pivotIndex(_ nums: [Int]) -> Int { 3 var numsLength = nums.count 4 guard numsLength != 0 else { return -1 } 5 6 var fromRight = Array(repeating: Int.min, count: numsLength) 7 fromRight[numsLength - 1] = nums[numsLength - 1] 8 var i = numsLength - 2 9 while i >= 0 { 10 fromRight[i] = nums[i] + fromRight[i + 1] 11 i -= 1 12 } 13 var fromLeft = 0 14 for i in 0..<(numsLength) { 15 var right = i + 1 < numsLength ? fromRight[i + 1] : 0 16 if fromLeft == right { 17 return i 18 } 19 fromLeft += nums[i] 20 21 } 22 return -1 23 } 24 }
172ms
1 class Solution { 2 func pivotIndex(_ nums: [Int]) -> Int { 3 var leftSum: Int = 0 4 var rightSum: Int = 0 5 6 for num in nums { 7 rightSum += num 8 } 9 10 for (index, value) in nums.enumerated() { 11 12 rightSum -= value 13 14 if leftSum == rightSum { 15 return index 16 } else { 17 leftSum += value 18 } 19 } 20 return -1 21 } 22 }
176ms
1 class Solution { 2 func pivotIndex(_ nums: [Int]) -> Int { 3 if nums.count <= 0{ 4 return -1 5 } 6 7 if nums.count == 1{ 8 return 0 9 } 10 11 // 計算和 12 var resunlt: Int = nums.reduce(0, +) 13 // 左邊 14 var left: Int = 0 15 16 for i in 0...nums.count - 1 { 17 18 resunlt -= nums[i] 19 if i == 0 { 20 if resunlt == 0 { 21 return i 22 } 23 }else{ 24 left += nums[i - 1]; 25 if resunlt == left{ 26 return i 27 } 28 } 29 } 30 return -1 31 } 32 }
177ms
1 class Solution { 2 func pivotIndex(_ nums: [Int]) -> Int { 3 var index = -1 4 var numsSum = 0 5 for i in nums { 6 numsSum += i 7 } 8 var sum = 0 9 for j in 0..<nums.count { 10 if (numsSum - nums[j])%2 == 0 { 11 if sum == (numsSum - nums[j])/2 { 12 index = j 13 break 14 } 15 } 16 sum += nums[j] 17 } 18 return index 19 } 20 }
180ms
1 class Solution { 2 func pivotIndex(_ nums: [Int]) -> Int { 3 if nums.isEmpty { return -1 } 4 var sum = nums.reduce(0,+) 5 var leftSum = 0 6 for (index,element) in nums.enumerated() { 7 if leftSum == (sum - leftSum) - element { return index } 8 leftSum += element 9 } 10 return -1 11 } 12 }
224ms
1 class Solution { 2 func pivotIndex(_ nums: [Int]) -> Int { 3 4 var total = nums.reduce(0, +) 5 var sum = 0 6 7 for i in 0..<nums.count { 8 total -= nums[i] 9 if sum == total { 10 return i 11 } 12 sum += nums[i] 13 } 14 return -1 15 } 16 }
225ms
1 class Solution { 2 func pivotIndex(_ nums: [Int]) -> Int { 3 guard !nums.isEmpty else { return -1 } 4 5 var prefixSums = Array(repeating: 0, count: nums.count) 6 7 prefixSums[0] = nums[0] 8 for (i, num) in nums.enumerated().dropFirst() { 9 prefixSums[i] = prefixSums[i - 1] + num 10 } 11 12 for (i, prefixSum) in prefixSums.enumerated() { 13 let leftSum = prefixSum - nums[i] 14 let rightSum = prefixSums.last! - nums[i] - leftSum 15 16 if leftSum == rightSum { 17 return i 18 } 19 } 20 21 return -1 22 } 23 }