-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path724.java
More file actions
42 lines (39 loc) · 1.26 KB
/
724.java
File metadata and controls
42 lines (39 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
public int pivotIndex(int[] nums) {
int left=0,right=0,index=0;
for (int i=1; i<nums.length; i++){
right = right+nums[i];
}
while (left!=right){
index++;
if (index==nums.length) break;
left = left+nums[index-1];
right = right-nums[index];
}
return (index==nums.length) ? -1 :index;
}
}
__________________________________________________________________________________________________
sample 39048 kb submission
class Solution {
public int pivotIndex(int[] nums) {
// TODO verify
if (nums.length == 1) {
return 0;
}
int totalSum = Arrays.stream(nums).sum();
int pivotIndex = 0;
int pivotSum = 0;
while (pivotIndex < nums.length) {
if ((float)(totalSum - nums[pivotIndex]) / 2 == pivotSum) {
return pivotIndex;
}
pivotSum += nums[pivotIndex];
pivotIndex++;
}
return -1;
}
}
__________________________________________________________________________________________________