We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent cf2f73b commit ce69c74Copy full SHA for ce69c74
java/57-Insert-Interval.java
@@ -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