合并数组范围golang版本
2023-08-20
1分钟阅读时长
合并数组范围
func merge(intervals [][]int) [][]int{
var res [][]int
sort.Slice(intervals,func(i,j int) bool{
return intervals[i][0] < intervals[j][0]
})
left,right := intervals[0][0],intervals[0][1]
for i :=1;i<len(intervals);i++{
if intervals[i][0] <= right{
if intervals[i][1] > right{
right = intervals[i][1]
}
}else{
res = append(res, []int{left,right})
left,right = intervals[i][0],intervals[i][1]
}
}
return append(res,[]int{left,right})
}
func merge(nums1 []int, m int, nums2 []int, n int) {
max := m + n - 1
i := m - 1
j := n - 1
for i >= 0 && j >= 0 {
if nums1[i] > nums2[j] {
nums1[max] = nums1[i]
i--
} else {
nums1[max] = nums2[j]
j--
}
}
for j >= 0 {
nums1[max] = nums2[j]
j--
max--
}
for i >= 0 {
nums1[max] = nums1[i]
i--
max--
}
}