diff --git a/typescript/src/summaryRanges/README.md b/typescript/src/summaryRanges/README.md index e93b9db..b05a7da 100644 --- a/typescript/src/summaryRanges/README.md +++ b/typescript/src/summaryRanges/README.md @@ -39,6 +39,7 @@ Explanation: The ranges are: ## Solution ```typescript +// Solution 1 function summaryRanges(nums: number[]): string[] { if (nums.length === 0) return []; @@ -68,6 +69,32 @@ function summaryRanges(nums: number[]): string[] { return result; } + +// Solution 2 +function summaryRanges(nums: number[]): string[] { + const result: string[] = []; + + let i = 0; + while (i < nums.length) { + let start = nums[i]; + let end = start; + + while (i + 1 < nums.length && nums[i + 1] === end + 1) { + end = nums[i + 1]; + i++; + } + + if (start === end) { + result.push(`${start}`); + } else { + result.push(`${start}->${end}`); + } + + i++; + } + + return result; +} ``` ## Complexity Analysis