-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathSolution.java
More file actions
24 lines (22 loc) · 752 Bytes
/
Solution.java
File metadata and controls
24 lines (22 loc) · 752 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package g0701_0800.s0724_find_pivot_index;
// #Easy #Array #Prefix_Sum #LeetCode_75_Prefix_Sum #Level_1_Day_1_Prefix_Sum
// #2022_03_24_Time_2_ms_(69.67%)_Space_52.1_MB_(59.19%)
public class Solution {
public int pivotIndex(int[] nums) {
if (nums == null || nums.length == 0) {
return -1;
}
int[] sums = new int[nums.length];
sums[0] = nums[0];
for (int i = 1; i < nums.length; i++) {
sums[i] = sums[i - 1] + nums[i];
}
for (int i = 0; i < nums.length; i++) {
int temp = sums[nums.length - 1] - sums[i];
if (i == 0 && 0 == temp || (i > 0 && sums[i - 1] == temp)) {
return i;
}
}
return -1;
}
}