## 找到数组中左右两边的和相等的pivot的下标 Find Pivot Index 原

叶枫啦啦

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]`.

①  找到左右两边的和相等的pivot的下标，如果有多个，则返回最左侧，如果不存在，返回-1.

class Solution { //34ms
public int pivotIndex(int[] nums) {
int sum = 0;
for (int n : nums){
sum += n;
}
int cursum = 0;
for (int i = 0;i < nums.length;i ++){
if (sum - nums[i] == 2 * cursum) return i;
cursum += nums[i];
}
return -1;
}
}

### 评论(0)

Find Pivot Index之Python实现

osc_dos38x1o
2018/09/27
1
0
LeetCode算法题-Find Pivot Index（Java实现）

2019/04/12
0
0
Java中的排序

Hosee
2016/04/04
381
0
java.util.DualPivotQuickSort的实现

DualPivotQuickSort汇集了多种排序算法，称之为DualPivotQuickSort并不合适。不同的排序算法有不同的使用场景。看懂此文件，排序算法就算彻底搞懂了。本文只介绍有用的代码片段，DualPivotQu...

osc_8zk7ewr4
2019/06/02
6
0
[LeetCode] 215. Kth Largest Element in an Array 数组中第k大的元素

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Example 1: Input: and k = 2Output: ......

osc_cym01vn6
2018/03/06
2
0

9
0
2048游戏的最佳算法是什么？ - What is the optimal algorithm for the game 2048?

javail

9
0
Spring Cloud Ribbon 客户端负载均衡

Ribbon客户端组件提供一系列完善的配置选项，比如连接超时、重试、重试算法等，内置可插拔、可定制的负载均衡组件。下面是用到的一些负载均衡策略： 简单轮询负载均衡 加权轮询负载均衡 区域...

7
0

9
0
OSChina 周四乱弹 —— 卖全家桶！

Osc乱弹歌单（2020）请戳（这里） 【今日歌曲】 @巴拉迪维 ：陈慧娴的单曲《与泪抱拥》 陈慧娴的嗓音加上向雪怀的词，这样的经典组合真不多。#今日歌曲推荐# 《与泪抱拥》- 陈慧娴 手机党少年...

15
0