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