## 找到数组中左右两边的和相等的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 = 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;
}
} ### 叶枫啦啦

Java中的排序

Hosee
2016/04/04
140
0

1.说明 快速排序法（quicksort）是目前所公认最快的排序方法之一（视解题的对象而定），虽然快速排序法在最差状况下可以达O(n2)，但是在多数的情况下，快速排序法的效率表现是相当不错的。快...

2017/12/14
0
0
LeetCode 33-Search in Rotated Sorted Array

Problem description Solution Self 看到这个题目，头脑中第一个蹦出来的就是找到rotated的元素，然后两边分别做binary search就可以了。 第一版代码 分两步：1：找到pivot，就是rotated的那...

Quan全
2018/06/21
0
0

2016/12/14
1
0

Jackson_Mu
2014/12/30
0
9

echojson
29分钟前
1
0
rabbitmq安装教程

RabbitMQ有Windows与Linux版本的，这里先写Windows版本的安装。 以前安装软件总是在百度上找某某安装教程，结果能按照教程安装好的软件真的不多。想起先前以为大牛说的一句话，去官网按照官网...

em_aaron

7
0
Android 贝塞尔曲线实践——波浪式运动

IamOkay

3
0
Nmap之防火墙/IDS逃逸

Frost729

2
0

java菜分享

8
0 