FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
interviews/leetcode/array/MergeIntervals.java at master · codingOnGithub/interviews · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
codingOnGithub
/
interviews
Public
forked from
kdn251/interviews
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
interviews
/
leetcode
/
array
/
MergeIntervals.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (38 loc) · 1.3 KB
Breadcrumbs
interviews
/
leetcode
/
array
/
MergeIntervals.java
Copy path
File metadata and controls
43 lines (38 loc) · 1.3 KB
Raw
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
43
// 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; }
* }
*/
class
MergeIntervals
{
public
List
<
Interval
>
merge
(
List
<
Interval
>
intervals
) {
List
<
Interval
>
result
=
new
ArrayList
<
Interval
>();
if
(
intervals
==
null
||
intervals
.
size
() ==
0
) {
return
result
;
}
Interval
[]
allIntervals
=
intervals
.
toArray
(
new
Interval
[
intervals
.
size
()]);
Arrays
.
sort
(
allIntervals
,
new
Comparator
<
Interval
>() {
public
int
compare
(
Interval
a
,
Interval
b
) {
if
(
a
.
start
==
b
.
start
) {
return
a
.
end
-
b
.
end
;
}
return
a
.
start
-
b
.
start
;
}
});
for
(
Interval
i
:
allIntervals
) {
if
(
result
.
size
() ==
0
||
result
.
get
(
result
.
size
() -
1
).
end
<
i
.
start
) {
result
.
add
(
i
);
}
else
{
result
.
get
(
result
.
size
() -
1
).
end
=
Math
.
max
(
result
.
get
(
result
.
size
() -
1
).
end
,
i
.
end
);
}
}
return
result
;
}
}
Back
|
FazBrowse Home
|
New Git URL