File tree 1 file changed +13
-15
lines changed 1 file changed +13
-15
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
- public List <String > summaryRanges (int [] nums ) {
3
- List <String > result = new ArrayList <>();
4
- int idx = 0 ;
5
- int n = nums .length ;
6
- while ( idx < n ) {
7
- int start = nums [ idx ] ;
8
- int end = start ;
9
- idx ++;
10
- while ( idx < n && nums [ idx ] == end + 1 ) {
11
- end = nums [ idx ];
12
- idx ++ ;
13
- }
14
- result . add ( start == end ? String . valueOf ( start ) : ( start + "->" + end )) ;
2
+ public List <String > summaryRanges (int [] nums ) {
3
+ List <String > result = new ArrayList <>();
4
+ int idx = 0 ;
5
+ while ( idx < nums .length ) {
6
+ int startIdx = idx ;
7
+ int endIdx = idx ;
8
+ idx ++ ;
9
+ while ( idx < nums . length && nums [ idx ] - nums [ idx - 1 ] == 1 ) {
10
+ endIdx = idx ++;
11
+ }
12
+ result . add ( startIdx == endIdx ? String . valueOf ( nums [ startIdx ]) : nums [ startIdx ] + "->" + nums [ endIdx ]) ;
13
+ }
14
+ return result ;
15
15
}
16
- return result ;
17
- }
18
16
}
You can’t perform that action at this time.
0 commit comments