Skip to content

Commit ec717ca

Browse files
committed
feat: add summary ranges solution
1 parent 2b0d5b7 commit ec717ca

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

typescript/src/summaryRanges/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Explanation: The ranges are:
3939
## Solution
4040

4141
```typescript
42+
// Solution 1
4243
function summaryRanges(nums: number[]): string[] {
4344
if (nums.length === 0) return [];
4445

@@ -68,6 +69,32 @@ function summaryRanges(nums: number[]): string[] {
6869

6970
return result;
7071
}
72+
73+
// Solution 2
74+
function summaryRanges(nums: number[]): string[] {
75+
const result: string[] = [];
76+
77+
let i = 0;
78+
while (i < nums.length) {
79+
let start = nums[i];
80+
let end = start;
81+
82+
while (i + 1 < nums.length && nums[i + 1] === end + 1) {
83+
end = nums[i + 1];
84+
i++;
85+
}
86+
87+
if (start === end) {
88+
result.push(`${start}`);
89+
} else {
90+
result.push(`${start}->${end}`);
91+
}
92+
93+
i++;
94+
}
95+
96+
return result;
97+
}
7198
```
7299

73100
## Complexity Analysis

0 commit comments

Comments
 (0)