---
# System prepended metadata

title: 0724. Find Pivot Index
tags: [Leetcode, FaceBook, Easy, Prefix Sum]

---

# 0724. Find Pivot Index
###### tags: `Leetcode` `Easy` `FaceBook` `Prefix Sum`
Link: https://leetcode.com/problems/find-pivot-index/
## 思路
算出sum之后，遍历
遍历的过程中左边的数的sum（prefix sum）如果等于右边数的sum（sum-prefix sum-arr[i]),则返回i
## Code
```java=
class Solution {
    public int pivotIndex(int[] nums) {
        int sum = nums[0];
        for(int i = 1;i < nums.length;i++){
            sum+=nums[i];
            // nums[i]+=nums[i-1];
        }
        int prefixSum = 0;
        int rightSum;
        for(int i = 0;i < nums.length;i++){
            prefixSum += i==0?0:nums[i-1];
            rightSum = sum-nums[i]-prefixSum;
            // System.out.println(prefixSum+" "+rightSum);
            if(prefixSum == rightSum){
                return i;
            }
        }
        return -1;
    }
}
```