Skip to content

Commit 317c3ad

Browse files
committed
Create: 0724-find-pivot-index.ts
1 parent ee7a3bc commit 317c3ad

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Diff for: typescript/0724-find-pivot-index.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function pivotIndex(nums: number[]): number {
2+
let total = nums.reduce((a, b) => a + b);
3+
let leftSum = 0;
4+
5+
for (let i = 0; i < nums.length; i++) {
6+
const rightSum = total - nums[i] - leftSum;
7+
8+
if (leftSum === rightSum) return i;
9+
leftSum += nums[i];
10+
}
11+
12+
return -1;
13+
}

0 commit comments

Comments
 (0)