-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_Intervals.java
More file actions
42 lines (37 loc) · 1.11 KB
/
Merge_Intervals.java
File metadata and controls
42 lines (37 loc) · 1.11 KB
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
56. Merge Intervals
Given a collection of intervals, merge all overlapping intervals.
For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public List<Interval> merge(List<Interval> intervals) {
Collections.sort(intervals, new Comparator<Interval>() {
public int compare(Interval a, Interval b) {
return a.start-b.start;
}
});
List<Interval> res = new ArrayList<Interval>();
int i=0;
while(i<intervals.size()) {
int start = intervals.get(i).start;
int end = intervals.get(i).end;
int j=i+1;
while(j<intervals.size() && intervals.get(j).start<=end) {
end = Math.max(end, intervals.get(j).end);
j++;
}
res.add(new Interval(start, end));
i = j;
}
return res;
}
}