Skip to content

Commit

Permalink
feat: add summary ranges solution
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrig committed Jul 25, 2023
1 parent 2b0d5b7 commit ec717ca
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions typescript/src/summaryRanges/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Explanation: The ranges are:
## Solution

```typescript
// Solution 1
function summaryRanges(nums: number[]): string[] {
if (nums.length === 0) return [];

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ec717ca

Please sign in to comment.