-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion228.java
More file actions
51 lines (45 loc) · 1020 Bytes
/
Copy pathQuestion228.java
File metadata and controls
51 lines (45 loc) · 1020 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
44
45
46
47
48
49
50
51
/*
Author: Ananthanarayanan R
Section: Algorithms
Question : 228
*/
import java.util.*;
public class Question228
{
public static List<String> summaryRanges(int[] nums)
{
List<String> result = new ArrayList<>();
if(nums.length==0)
return result;
if(nums.length==1)
{
result.add(""+nums[0]);
return result;
}
int currValue = nums[0];
int startValue = currValue;
for(int i = 1;i<nums.length;i++)
if(nums[i]==currValue+1)
currValue++;
else
{
if(startValue==currValue)
result.add(""+startValue);
else
result.add(""+startValue+"->"+currValue);
startValue = nums[i];
currValue = startValue;
}
if(startValue==currValue)
result.add(""+startValue);
else
result.add(""+startValue+"->"+currValue);
return result;
}
public static void main(String[] args)
{
int[] nums = {0,1,2,3,5,6,8,9,10,12,13};
List<String> result = summaryRanges(nums);
System.out.println(result);
}
}