Skip to content

Commit 777670d

Browse files
author
谭佳胜
committed
Find Pivot Index
1 parent 7eb2fef commit 777670d

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

724. Find Pivot Index/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const pivotIndex = function(nums) {
2+
if (nums.length === 0) return -1;
3+
const sum = nums.reduce((pre, cur) => pre + cur);
4+
let currentSum = 0;
5+
6+
for (let i = 0; i < nums.length; i++) {
7+
if (sum - 2 * currentSum === nums[i]) return i;
8+
currentSum += nums[i];
9+
}
10+
11+
return -1;
12+
};

0 commit comments

Comments
 (0)