We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f3e8172 commit c6e6303Copy full SHA for c6e6303
228. Summary Ranges.java
@@ -0,0 +1,31 @@
1
+public class Solution {
2
+ public List<String> summaryRanges(int[] nums) {
3
+ if (nums.length == 0) {
4
+ return new ArrayList<String>();
5
+ }
6
+
7
+ List<String> result = new ArrayList<>();
8
+ int i = 0;
9
10
+ while (i < nums.length) {
11
+ if((i == nums.length - 1) || (nums[i+1] > nums[i] + 1)) {
12
+ result.add(String.valueOf(nums[i]));
13
+ i += 1;
14
+ } else {
15
+ int j = i + 1;
16
+ int prev = nums[i];
17
+ while (nums[j] == prev + 1) {
18
+ prev = nums[j];
19
+ j += 1;
20
+ if (j == nums.length) break;
21
22
23
+ result.add(String.valueOf(nums[i]) + "->" + String.valueOf(prev));
24
+ i = j;
25
26
27
28
29
+ return result;
30
31
+}
0 commit comments