File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed
typescript/src/summaryRanges Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,7 @@ Explanation: The ranges are:
39
39
## Solution
40
40
41
41
``` typescript
42
+ // Solution 1
42
43
function summaryRanges(nums : number []): string [] {
43
44
if (nums .length === 0 ) return [];
44
45
@@ -68,6 +69,32 @@ function summaryRanges(nums: number[]): string[] {
68
69
69
70
return result ;
70
71
}
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
+ }
71
98
```
72
99
73
100
## Complexity Analysis
You can’t perform that action at this time.
0 commit comments