From 584654ba7ef0a791391ffb7745a52c23cf0981c0 Mon Sep 17 00:00:00 2001 From: lynn <435962415@qq.com> Date: Thu, 23 Mar 2023 10:57:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=860056.=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E5=8C=BA=E9=97=B4=E7=9A=84=E8=A7=A3=E6=B3=95=E4=BA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...10\345\271\266\345\214\272\351\227\264.md" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" index d467ab1a29..0daf5f9b2c 100644 --- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" +++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" @@ -174,6 +174,34 @@ func max(a, b int) int { return b } ``` +```go +// 版本2 +func merge(intervals [][]int) [][]int { + if len(intervals) == 1 { + return intervals + } + sort.Slice(intervals, func(i, j int) bool { + return intervals[i][0] < intervals[j][0] + }) + res := make([][]int, 0) + res = append(res, intervals[0]) + for i := 1; i < len(intervals); i++ { + if intervals[i][0] <= res[len(res)-1][1]{ + res[len(res)-1][1] = max56(res[len(res)-1][1],intervals[i][1]) + } else { + res = append(res, intervals[i]) + } + } + return res +} +func max56(a, b int) int { + if a > b { + return a + } + return b +} +``` + ### Javascript ```javascript