-
Notifications
You must be signed in to change notification settings - Fork 43
/
SummaryRanges.java
66 lines (57 loc) · 1.87 KB
/
SummaryRanges.java
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package LeetCodeJava.Array;
// https://leetcode.com/problems/summary-ranges/
import java.util.ArrayList;
import java.util.List;
public class SummaryRanges {
// V0
// V1
// https://leetcode.com/problems/summary-ranges/solutions/1805391/concise-solution-in-0-n-with-approach-explained-in-detail/
public List<String> summaryRanges(int[] nums) {
ArrayList<String> al = new ArrayList<>();
for(int i=0;i<nums.length;i++){
int start=nums[i];
while(i+1<nums.length && nums[i]+1==nums[i+1]){
i++;
}
if(start!=nums[i]){
al.add(""+start+"->"+nums[i]);
}
else{
al.add(""+start);
}
}
return al;
}
// V2
// https://leetcode.com/problems/summary-ranges/solutions/2813052/java-100-faster-solution/
public List<String> summaryRanges_2(int[] nums) {
List<String> list = new ArrayList<>();
if (nums.length == 0) return list;
int start = nums[0], end = nums[0];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] + 1 != nums[i + 1]) {
if (start == end) {
sb.append(start);
list.add(sb.toString());
} else {
sb.append(start).append("->").append(end);
list.add(sb.toString());
}
sb.setLength(0);
start = nums[i + 1];
end = nums[i + 1];
} else {
end = nums[i + 1];
}
}
if (start == end) {
sb.append(start);
list.add(sb.toString());
} else {
sb.append(start).append("->").append(end);
list.add(sb.toString());
}
return list;
}
}