-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path23rdJune(I).java
More file actions
43 lines (37 loc) · 985 Bytes
/
Copy path23rdJune(I).java
File metadata and controls
43 lines (37 loc) · 985 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
33
34
35
36
37
38
39
40
41
42
43
import java.util.ArrayList;
import java.util.List;
public class rough{
static List<String> summaryRanges(int[] nums){
List<String> res = new ArrayList<>();
int i = 0;
int j = 0;
while(i<nums.length){
while(j!=nums.length-1){
if(nums[j+1]==nums[j]+1){
j+=1;
}
else{
break;
}
}
if(i==j){
res.add(""+nums[j]);
}
else{
res.add(nums[i]+"->"+nums[j]);
}
i=j+1;
j=i;
}
return res;
}
public static void main(String[] args) {
int[] nums = {0,1,2,4,5,7};
System.out.println(summaryRanges(nums));
// Output: ["0->2","4->5","7"]
// Explanation: The ranges are:
// [0,2] --> "0->2"
// [4,5] --> "4->5"
// [7,7] --> "7"
}
}