forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsora0319.java
More file actions
32 lines (27 loc) · 933 Bytes
/
sora0319.java
File metadata and controls
32 lines (27 loc) · 933 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
31
public class Solution {
public int[][] insert(int[][] intervals, int[] newInterval) {
List<int[]> list = new ArrayList<>();
int order = 0;
while (order < intervals.length && intervals[order][0] < newInterval[0]) {
list.add(intervals[order]);
order++;
}
list.add(newInterval);
while (order < intervals.length) {
list.add(intervals[order]);
order++;
}
List<int[]> output = new ArrayList<>();
output.add(list.get(0));
for (int i = 1; i < list.size(); i++) {
int[] last = output.get(output.size() - 1);
int[] current = list.get(i);
if (last[1] < current[0]) {
output.add(current);
} else {
last[1] = Math.max(last[1], current[1]);
}
}
return output.toArray(new int[output.size()][]);
}
}