ðŸ¥ˆ

# Merge Intervals

Created
2021/12/24 02:46
ë¬¸ì œ ë²ˆí˜¸
56
ì¹´í…Œê³ ë¦¬
Array
Sorting

### Code

 .css-15tnwsa{max-width:100%;width:100%;white-space:pre-wrap;word-break:break-word;padding:7px 9px;background-color:transparent;font-size:14px;line-height:20px;min-height:1em;}.css-15tnwsa:empty::after{content:" ";}ì œì¶œ ë‚ ì§œ ì‹œê°„ ë©”ëª¨ë¦¬ 2021/12/24 4 ms 4.5 MB
// 56. Merge Intervals // // https://leetcode.com/problems/merge-intervals/ // sort package imported to order the interval slices import ( "sort" ) // merge function literally merge the intervals. // [[1, 3], [2, 6], [8, 10], [15, 18]] will be [[1, 6], [8, 10], [15, 18]]. // To merge, compare the start and the end should be easy. Sorting with the start point is necessary. // After that, just compare the following if statement and update. // No need to use a new [][]int, but adopted the way to make the code clear. func merge(intervals [][]int) [][]int { sort.Slice(intervals, func(i, j int) bool { return intervals[i][0] < intervals[j][0] }) res := [][]int{} for i, interval := range intervals { if i == 0 { res = append(res, interval) continue } temp := res[len(res)-1] if temp[1] >= interval[0] && temp[1] < interval[1] { res[len(res)-1] = []int{temp[0], interval[1]} } else if temp[1] < interval[0] { res = append(res, interval) } } return res }
Go
ë³µì‚¬