- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution228.java
More file actions
Latest commit
32 lines (28 loc) · 859 Bytes
/
Copy pathSolution228.java
File metadata and controls
32 lines (28 loc) · 859 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
importjava.util.ArrayList;
importjava.util.List;
publicclassSolution228 {
publicList<String> summaryRanges(int[] nums) {
Listans = newArrayList();
intlength = nums.length;
intl = 0;
intr = 0;
while (r < length) {
while (r < length - 1 && nums[r] + 1 == nums[r + 1]) {
++r;
}
if (l != r) {
ans.add(Integer.toString(nums[l]) + "->" + Integer.toString(nums[r]));
} else {
ans.add(Integer.toString(nums[l]));
}
++r;
l = r;
}
returnans;
}
publicstaticvoidmain(String[] args) {
Solution228solution228 = newSolution228();
int[] nums = newint[]{0,2,3,4,6,8,9};
System.out.println(solution228.summaryRanges(nums));
}
}