题目: 给定两个增序如数组 [1,2,2,3,4,5,5] [2,3,4,5,5,5],要求输出[1,2,5] .求求两个数组diff
package main
import (
"fmt"
)
func diff(arr1 []int, arr2 []int) []int {
len1 := len(arr1)
len2 := len(arr2)
i := 0
j := 0
arrRes := []int{}
for i < len1 && j < len2 {
fmt.Println(arr1[i], arr2[j])
if arr1[i] == arr2[j] {
i++
j++
continue
} else if arr1[i] > arr2[j] {
arrRes = append(arrRes, arr2[j])
j++
} else {
arrRes = append(arrRes, arr1[i])
i++
}
}
if len1 > len2 {
for ; i < len1; i++ {
arrRes = append(arrRes, arr1[i])
}
}
if len2 > len1 {
for ; i < len1; i++ {
arrRes = append(arrRes, arr2[i])
}
}
return arrRes
}
func main() {
arr1 := []int{1, 2, 2, 3, 4, 5, 5, 5, 5, 7}
arr2 := []int{2, 3, 5, 5}
fmt.Println(diff(arr1, arr2))
}
网友评论