LeetCode Note Java 00724:Find Pivot Index
找出輸入的 intArray nums
的 Pivot Index (索引的左右兩邊總和相同)。
題目
Find Pivot Index Easy
Given an array of integers nums, calculate the pivot index of this array.
The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index’s right.
If the index is on the left edge of the array, then the left sum is 0 because there are no elements to the left. This also applies to the right edge of the array.
Return the leftmost pivot index. If no such index exists, return -1.
我的解法
1 | class Solution { |
用了粗暴方式作答,Runtime 拉得很長。
檢討
別人寫得 Runtime 就極短。
1 | // Runtime: 1 ms, faster than 92.94% of Java online submissions for Find Pivot Index. |
避開了多層回圈,果然還是要多看別人的解答來擴編我僵化的腦袋。
參考資料
Very Easy || 100% || Fully Explained || Java, C++, Python, JS, Python3