Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
42 lines (41 loc) · 1.18 KB

File metadata and controls

42 lines (41 loc) · 1.18 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/**
* 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; }
* }
*/
class IntervalComparator implements Comparator<Interval> {
@Override
public int compare(Interval a, Interval b) {
if (a.start > b.start) {
return 1;
} else if (a.start < b.start) {
return -1;
} else {
return 0;
}
}
}
public class Solution {
public List<Interval> merge(List<Interval> intervals) {
Collections.sort(intervals, new IntervalComparator());
List<Interval> result = new ArrayList<Interval>();
if (intervals.size() == 0) {
return result;
}
result.add(intervals.get(0));
int i = 1;
while (i < intervals.size()) {
if (intervals.get(i).start > result.get(result.size()-1).end) {
result.add(intervals.get(i));
} else {
result.get(result.size()-1).end = Math.max(result.get(result.size()-1).end, intervals.get(i).end);
}
i ++;
}
return result;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.