leetcode-master/problems/0724.寻找数组的中心索引.md

166 lines
4.5 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
# 724.寻找数组的中心下标
[力扣题目链接](https://leetcode.cn/problems/find-pivot-index/)
给你一个整数数组 nums ,请计算数组的 中心下标 。
数组 中心下标 是数组的一个下标,其左侧所有元素相加的和等于右侧所有元素相加的和。
如果中心下标位于数组最左端,那么左侧数之和视为 0 ,因为在下标的左侧不存在元素。这一点对于中心下标位于数组最右端同样适用。
如果数组有多个中心下标,应该返回 最靠近左边 的那一个。如果数组不存在中心下标,返回 -1 。
示例 1
* 输入nums = [1, 7, 3, 6, 5, 6]
* 输出3
* 解释:中心下标是 3。左侧数之和 sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11 ,右侧数之和 sum = nums[4] + nums[5] = 5 + 6 = 11 ,二者相等。
示例 2
* 输入nums = [1, 2, 3]
* 输出:-1
* 解释:数组中不存在满足此条件的中心下标。
示例 3
* 输入nums = [2, 1, -1]
* 输出0
* 解释:中心下标是 0。左侧数之和 sum = 0 ,(下标 0 左侧不存在元素),右侧数之和 sum = nums[1] + nums[2] = 1 + -1 = 0 。
## 思路
这道题目还是比较简单直接的
1. 遍历一遍求出总和sum
2. 遍历第二遍求中心索引左半和leftSum
* 同时根据sum和leftSum 计算中心索引右半和rightSum
* 判断leftSum和rightSum是否相同
C++代码如下:
```CPP
class Solution {
public:
int pivotIndex(vector<int>& nums) {
int sum = 0;
for (int num : nums) sum += num; // 求和
int leftSum = 0; // 中心索引左半和
int rightSum = 0; // 中心索引右半和
for (int i = 0; i < nums.size(); i++) {
leftSum += nums[i];
rightSum = sum - leftSum + nums[i];
if (leftSum == rightSum) return i;
}
return -1;
}
};
```
## 其他语言版本
### Java
```java
class Solution {
public int pivotIndex(int[] nums) {
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i]; // 总和
}
int leftSum = 0;
int rightSum = 0;
for (int i = 0; i < nums.length; i++) {
leftSum += nums[i];
rightSum = sum - leftSum + nums[i]; // leftSum 里面已经有 nums[i],多减了一次,所以加上
if (leftSum == rightSum) {
return i;
}
}
return -1; // 不存在
}
}
```
### Python3
```python
class Solution:
def pivotIndex(self, nums: List[int]) -> int:
numSum = sum(nums) #数组总和
leftSum = 0
for i in range(len(nums)):
if numSum - leftSum -nums[i] == leftSum: #左右和相等
return i
leftSum += nums[i]
return -1
```
### Go
```go
func pivotIndex(nums []int) int {
sum := 0
for _, v := range nums {
sum += v;
}
leftSum := 0 // 中心索引左半和
rightSum := 0 // 中心索引右半和
for i := 0; i < len(nums); i++ {
leftSum += nums[i]
rightSum = sum - leftSum + nums[i]
if leftSum == rightSum{
return i
}
}
return -1
}
```
### JavaScript
```js
var pivotIndex = function(nums) {
const sum = nums.reduce((a,b) => a + b);//求和
// 中心索引左半和 中心索引右半和
let leftSum = 0, rightSum = 0;
for(let i = 0; i < nums.length; i++){
leftSum += nums[i];
rightSum = sum - leftSum + nums[i];// leftSum 里面已经有 nums[i],多减了一次,所以加上
if(leftSum === rightSum) return i;
}
return -1;
};
```
### TypeScript
```typescript
function pivotIndex(nums: number[]): number {
const length: number = nums.length;
const sum: number = nums.reduce((a, b) => a + b);
let leftSum: number = 0;
for (let i = 0; i < length; i++) {
const rightSum: number = sum - leftSum - nums[i];
if (leftSum === rightSum) return i;
leftSum += nums[i];
}
return -1;
};
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>