-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0056-Merge-intervals.cs
More file actions
40 lines (31 loc) · 1015 Bytes
/
0056-Merge-intervals.cs
File metadata and controls
40 lines (31 loc) · 1015 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
32
33
34
35
36
37
38
39
40
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0056.Merge_intervals
{
public class _0056_Merge_intervals
{
public int[][] Merge(int[][] intervals)
{
if (intervals == null || intervals.Length == 0) return null;
Array.Sort(intervals, (m, n) => m[0].CompareTo(n[0]));
List<int[]> res = new List<int[]>();
int start = intervals[0][0];
int end = intervals[0][1];
for (int i = 1; i < intervals.Length; i++)
{
if (intervals[i][0] <= end)
{
end = Math.Max(end, intervals[i][1]);
continue;
}
res.Add(new int[] { start, end });
start = intervals[i][0];
end = Math.Max(end, intervals[i][1]);
}
// last item
res.Add(new int[] { start, end });
return res.ToArray();
}
}
}