From 98827c14d0efdceaeb6ebed14f82c4c196e01f5c Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Mon, 8 Jan 2024 10:10:12 +0800 Subject: [PATCH] =?UTF-8?q?Update=200056.=E5=90=88=E5=B9=B6=E5=8C=BA?= =?UTF-8?q?=E9=97=B4=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0056.合并区间.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/0056.合并区间.md b/problems/0056.合并区间.md index 95781b1a..122e783a 100644 --- a/problems/0056.合并区间.md +++ b/problems/0056.合并区间.md @@ -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> res = new List>(); + 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(); + } +} +```