Update 0056.合并区间,添加C#

This commit is contained in:
eeee0717
2024-01-08 10:10:12 +08:00
parent 20f331f904
commit 98827c14d0

View File

@ -336,6 +336,32 @@ impl Solution {
}
}
```
### C#
```csharp
public class Solution
{
public int[][] Merge(int[][] intervals)
{
if (intervals.Length == 0)
return intervals;
Array.Sort(intervals, (a, b) => a[0] - b[0]);
List<List<int>> res = new List<List<int>>();
res.Add(intervals[0].ToList());
for (int i = 1; i < intervals.Length; i++)
{
if (res[res.Count - 1][1] >= intervals[i][0])
{
res[res.Count - 1][1] = Math.Max(res[res.Count - 1][1], intervals[i][1]);
}
else
{
res.Add(intervals[i].ToList());
}
}
return res.Select(x => x.ToArray()).ToArray();
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">