We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7eb2fef commit 777670dCopy full SHA for 777670d
724. Find Pivot Index/index.js
@@ -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