Skip to content

Commit 2c9e60e

Browse files
authored
Create Solution.java
1 parent b95ab1a commit 2c9e60e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int pivotIndex(int[] nums) {
3+
if (nums == null || nums.length < 3) {
4+
return -1;
5+
}
6+
int sum = Arrays.stream(nums).sum();
7+
int s = 0;
8+
for (int i = 0; i < nums.length; ++i) {
9+
if (s << 1 == sum - nums[i]) {
10+
return i;
11+
}
12+
s += nums[i];
13+
}
14+
return -1;
15+
}
16+
}

0 commit comments

Comments
 (0)