Skip to content

Commit 8789804

Browse files
committed
Updated Summary Ranges.java
1 parent e84f885 commit 8789804

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

Easy/Summary Ranges.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
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;
1515
}
16-
return result;
17-
}
1816
}

0 commit comments

Comments
 (0)