-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode_00057.java
More file actions
30 lines (27 loc) · 908 Bytes
/
LeetCode_00057.java
File metadata and controls
30 lines (27 loc) · 908 Bytes
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
package com.github.jerring.leetcode;
import java.util.ArrayList;
import java.util.List;
public class LeetCode_00057 {
public int[][] insert(int[][] intervals, int[] newInterval) {
List<int[]> res = new ArrayList<>();
boolean added = false;
for (int[] interval : intervals) {
if (interval[1] < newInterval[0]) {
res.add(interval);
} else if (interval[0] > newInterval[1]) {
if (!added) {
added = true;
res.add(newInterval);
}
res.add(interval);
} else {
newInterval[0] = Math.min(newInterval[0], interval[0]);
newInterval[1] = Math.max(newInterval[1], interval[1]);
}
}
if (!added) {
res.add(newInterval);
}
return res.toArray(new int[0][]);
}
}