본문 바로가기
Problem Solving/LeetCode

[LeetCode] 724. Find Pivot Index - Java

by graycode 2026. 10. 4.

• 문제 링크

 

Find Pivot Index - LeetCode

Can you solve this real interview question? Find Pivot Index - 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

leetcode.com

 

• 풀이 코드

public class Solution {

    public int pivotIndex(int[] nums) {
        int l = 0, r = 0;
        for (int i : nums) r += i;
        for (int i = 0; i < nums.length; l += nums[i++]) if (l == r - l - nums[i]) return i;

        return -1;
    }

}

댓글