diff --git a/problems/0724.寻找数组的中心索引.md b/problems/0724.寻找数组的中心索引.md index 14dcd2c0..0052e5d4 100644 --- a/problems/0724.寻找数组的中心索引.md +++ b/problems/0724.寻找数组的中心索引.md @@ -140,6 +140,24 @@ var pivotIndex = function(nums) { }; ``` +### TypeScript + +```typescript +function pivotIndex(nums: number[]): number { + const length: number = nums.length; + const sum: number = nums.reduce((a, b) => a + b); + let leftSum: number = 0; + for (let i = 0; i < length; i++) { + const rightSum: number = sum - leftSum - nums[i]; + if (leftSum === rightSum) return i; + leftSum += nums[i]; + } + return -1; +}; +``` + + + -----------------------