Skip to content

Commit af05c08

Browse files
committed
solve merge intervals
1 parent 9e742d9 commit af05c08

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

merge-intervals/sora0319.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Solution {
2+
public int[][] merge(int[][] intervals) {
3+
List<int[]> output = new ArrayList<>();
4+
5+
Arrays.sort(intervals, (a, b) -> a[0]- b[0]);
6+
7+
for (int[] interval : intervals) {
8+
if (output.isEmpty() || output.get(output.size() - 1)[1] < interval[0]) {
9+
output.add(interval);
10+
} else {
11+
output.get(output.size() - 1)[1] = Math.max(output.get(output.size() - 1)[1], interval[1]);
12+
}
13+
}
14+
15+
return output.toArray(new int[output.size()][]);
16+
}
17+
}
18+

0 commit comments

Comments
 (0)