Skip to content

Commit ce69c74

Browse files
Create 57-Insert-Interval.java
1 parent cf2f73b commit ce69c74

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

java/57-Insert-Interval.java

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

0 commit comments

Comments
 (0)