forked from yuanx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeIntervals.java
More file actions
51 lines (44 loc) · 1.39 KB
/
MergeIntervals.java
File metadata and controls
51 lines (44 loc) · 1.39 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
43
44
45
46
47
48
49
50
import java.util.Comparator;
import java.util.Collections;
/**
* 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 ArrayList<Interval> merge(ArrayList<Interval> intervals) {
// Start typing your Java solution below
// DO NOT write main() function
int len = intervals.size();
ArrayList<Interval> re = new ArrayList<Interval>();
if(len==0) return re;
Collections.sort(intervals, Int_compare);
Interval travel = intervals.get(0);
if(len==1){
re.add(travel);
return re;
}
for(int i=1; i<len; i++){
Interval temp = intervals.get(i);
if(temp.end<travel.start || temp.start > travel.end){
re.add(travel);
travel = temp;
}
else{
travel.start = Math.min(travel.start, temp.start);
travel.end = Math.max(travel.end, temp.end);
}
}
re.add(travel);
return re;
}
static final Comparator<Interval> Int_compare = new Comparator<Interval>(){
public int compare(Interval i, Interval j){
return new Integer(i.start).compareTo(new Integer(j.start));
}
};
}