# Leetcode [No. 2485] Find the Pivot Integer (EASY) 解題心得 ## 題目 https://leetcode.com/problems/find-the-pivot-integer/description/ ## 思路 + 這個解法就是用兩個陣列去紀錄每一個integer前後之和,如果preSum==postSum,就代表這個i是pivot。 ```c++= class Solution { public: int pivotInteger(int n) { vector<int> preSum(n+1,0); vector<int> postSum(n+1,0); // calculate preSum int pre = 0, post = 0; for (int i = 0 ; i <= n ; i++) { preSum[i] = pre; pre += i; } for (int i = n ; i >= 0; i--) { postSum[i] = post; post+= i; } for (int i = 0 ; i <= n; i++) { // cout << preSum[i] << "\t" << postSum[i] << endl; if(preSum[i] == postSum[i]) return i; } return -1; } }; ``` ### 解法分析 + time complexity: O(n) ### 執行結果 ![image](https://hackmd.io/_uploads/BJZDAi0pp.png)