教日月换新天。为有牺牲多壮志,敢

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

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

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 }
复制代码

 

 

posted @   为敢技术  阅读(420)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°