-
Notifications
You must be signed in to change notification settings - Fork 0
/
Merge Intervals.java
48 lines (46 loc) · 1.35 KB
/
Merge Intervals.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
/**
* 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
Collections.sort(intervals, new Comparator<Interval>()
{
public int compare(Interval a, Interval b)
{
return new Integer(a.start).compareTo(new Integer(b.start));
}
});
ArrayList<Interval> res=new ArrayList<Interval>();
if(intervals.size()==0) return res;
Interval first=new Interval(intervals.get(0).start,intervals.get(0).end);
if(intervals.size()==1)
{
res.add(first);
return res;
}
int i=1;
while(i<intervals.size())
{
if(intervals.get(i).start>first.end)
{
res.add(first);
first=intervals.get(i);
}
else if(intervals.get(i).end>first.end)
{
first.end=intervals.get(i).end;
}
i++;
}
res.add(first);
return res;
}
}