[Swift]LeetCode724. 寻找数组的中心索引 | Find Pivot Index

it2022-05-06  0

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址: https://www.cnblogs.com/strengthen/p/10514420.html ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Given an array of integers nums, write a method that returns the "pivot" index of this array.

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.

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:

Input: nums = [1, 2, 3] Output: -1 Explanation: There is no index that satisfies the conditions in the problem statement.

Note:

The length of nums will be in the range [0, 10000].Each element nums[i] will be an integer in the range [-1000, 1000].

给定一个整数类型的数组 nums,请编写一个能够返回数组“中心索引”的方法。

我们是这样定义数组中心索引的:数组中心索引的左侧所有元素相加的和等于右侧所有元素相加的和。

如果数组不存在中心索引,那么我们应该返回 -1。如果数组有多个中心索引,那么我们应该返回最靠近左边的那一个。

示例 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 }
Runtime: 144 ms Memory Usage: 19 MB 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 }

 

 

转载于:https://www.cnblogs.com/strengthen/p/10514420.html


最新回复(0)