forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
29 lines (24 loc) · 797 Bytes
/
Solution.cs
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
using System;
using System.Collections.Generic;
public class Solution {
public int[][] Insert(int[][] intervals, int[] newInterval) {
var result = new List<int[]>();
var i = 0;
while (i < intervals.Length && intervals[i][1] < newInterval[0])
{
result.Add(intervals[i++]);
}
while (i < intervals.Length && intervals[i][0] <= newInterval[1] && intervals[i][1] >= newInterval[0])
{
newInterval[0] = Math.Min(intervals[i][0], newInterval[0]);
newInterval[1] = Math.Max(intervals[i][1], newInterval[1]);
++i;
}
result.Add(newInterval);
while (i < intervals.Length)
{
result.Add(intervals[i++]);
}
return result.ToArray();
}
}