
-Code
class Solution {
public int pivotIndex(int[] nums) {
int numsLength = nums.length;
int left = 0, right = 0;
// 피벗이 0이므로 1부터 더하기
for (int i = 1; i < numsLength; i++) {
right += nums[i];
}
// 피벗을 옮기면서 계산
for (int i = 0; i < numsLength; i++) {
// 같으면 idx 반환
if (left == right) {
return i;
}
left += nums[i];
right -= i == numsLength - 1 ? 0 : nums[i + 1];
}
// 같은 값이 없으면 -1
return -1;
}
}'Beakjoon&프로그래머스 > Java' 카테고리의 다른 글
| [LeetCode/Java] Unique Number of Occurrences (0) | 2025.12.31 |
|---|---|
| [LeetCode/Java] Find the Difference of Two Arrays (0) | 2025.12.31 |
| [LeetCode/Java] Find the Highest Altitude (0) | 2025.12.31 |
| [LeetCode/Java] Maximum Average Subarray I (0) | 2025.12.31 |
| [LeetCode/Java] Is Subsequence (0) | 2025.12.31 |