mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 18:34:42 +01:00
20201117
This commit is contained in:
66
20201117/main.go
Normal file
66
20201117/main.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
//56. 合并区间
|
||||
//
|
||||
//给出一个区间的集合,请合并所有重叠的区间。
|
||||
//
|
||||
//
|
||||
//示例 1:
|
||||
//
|
||||
//输入: intervals = [[1,3],[2,6],[8,10],[15,18]]
|
||||
//输出: [[1,6],[8,10],[15,18]]
|
||||
//解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
|
||||
//示例 2:
|
||||
//
|
||||
//输入: intervals = [[1,4],[4,5]]
|
||||
//输出: [[1,5]]
|
||||
//解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。
|
||||
//注意:输入类型已于2019年4月15日更改。 请重置默认代码定义以获取新方法签名。
|
||||
//
|
||||
//提示:
|
||||
//intervals[i][0] <= intervals[i][1]
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
|
||||
func mergeAndSortSlices(src [][]int) []int {
|
||||
var ret []int
|
||||
for _, s := range src {
|
||||
for _, n := range s {
|
||||
ret = append(ret, n)
|
||||
}
|
||||
}
|
||||
sort.Ints(ret)
|
||||
return ret
|
||||
}
|
||||
|
||||
func pickInterval(src []int) [][]int {
|
||||
left := 0
|
||||
i := 1
|
||||
var ret [][]int
|
||||
for {
|
||||
if i+1 > len(src)-1 {
|
||||
if left < len(src) {
|
||||
ret = append(ret, [][]int{{src[left], src[i]}}...)
|
||||
}
|
||||
break
|
||||
}
|
||||
if src[i]+1 < src[i+1] {
|
||||
ret = append(ret, [][]int{{src[left], src[i+1]}}...)
|
||||
i += 2
|
||||
left = i
|
||||
continue
|
||||
}
|
||||
i++
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func merge(src [][]int) [][]int {
|
||||
return pickInterval(mergeAndSortSlices(src))
|
||||
}
|
||||
103
20201117/main_test.go
Normal file
103
20201117/main_test.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_mergeSlices(t *testing.T) {
|
||||
type args struct {
|
||||
src [][]int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want []int
|
||||
}{
|
||||
{
|
||||
"case_1",
|
||||
args{[][]int{{1, 3}, {2, 6}, {8, 10}, {15, 18}}},
|
||||
[]int{1, 2, 3, 6, 8, 10, 15, 18},
|
||||
},
|
||||
{
|
||||
"case_2",
|
||||
args{[][]int{{1, 4}, {4, 5}}},
|
||||
[]int{1, 4, 4, 5},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := mergeAndSortSlices(tt.args.src); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("mergeAndSortSlices() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_pickInterval(t *testing.T) {
|
||||
type args struct {
|
||||
src []int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want [][]int
|
||||
}{
|
||||
{
|
||||
"case_1",
|
||||
args{[]int{1, 2, 3, 6, 8, 10, 15, 18}},
|
||||
[][]int{{1, 6}, {8, 10}, {15, 18}},
|
||||
},
|
||||
{
|
||||
"case_2",
|
||||
args{[]int{1, 1}},
|
||||
[][]int{{1, 1}},
|
||||
},
|
||||
{
|
||||
"case_3",
|
||||
args{[]int{1, 2, 2, 3, 6, 8, 10, 15, 18}},
|
||||
[][]int{{1, 6}, {8, 10}, {15, 18}},
|
||||
},
|
||||
{
|
||||
"case_4",
|
||||
args{[]int{1, 4, 4, 5}},
|
||||
[][]int{{1, 5}},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := pickInterval(tt.args.src); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("pickInterval() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_merge(t *testing.T) {
|
||||
type args struct {
|
||||
src [][]int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want [][]int
|
||||
}{
|
||||
{
|
||||
"case_1",
|
||||
args{[][]int{{1, 3}, {2, 6}, {8, 10}, {15, 18}}},
|
||||
[][]int{{1, 6}, {8, 10}, {15, 18}},
|
||||
},
|
||||
{
|
||||
"case_2",
|
||||
args{[][]int{{1, 4}, {4, 5}}},
|
||||
[][]int{{1, 5}},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := merge(tt.args.src); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("merge() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user