---
# System prepended metadata

title: LeetCode 724. Find Pivot Index
tags: [Leetcode]

---

# LeetCode 724. Find Pivot Index
[LeetCode 724. Find Pivot Index](https://leetcode.com/problems/find-pivot-index/submissions/1317400360/) (難度 通過率)
<!-- 
(<font color=#00AF9B>Easy</font> 53.8%) 
(<font color=#FFB800>Medium</font> 39.6%)
(<font color=#FF375F>Hard</font>)
 -->
    
- 限制 :

    <ul>
    <li><code>1 <= nums.length <= 10^4</code></li>
    <li><code>-1000 <= nums[i] <= 1000</code></li>
    </ul>

- Solution
    這題的主要概念是要去算 pivot 左右邊(不算 pivot 本身!!) 的數列總合為多少，看清楚之後就會比較好寫。
    - 時間複雜度: $O(N)$
    - 空間複雜度: $O(N)$

- 程式碼
    ```c++=
    class Solution {
    public:
        int pivotIndex(vector<int>& nums) {
            vector<int> left_record{0};
            for (int i = 0; i < nums.size(); i++) {
                left_record.push_back(left_record.back() + nums[i]);
            }

            for (int i = 0; i < left_record.size() - 1; i++) {
                if (left_record[i] == left_record.back() - left_record[i + 1])
                    return i;
            }
            return -1;
        }
    };
    ```
</details>