题解 | #合并两个有序的数组#
合并两个有序的数组
http://www.nowcoder.com/practice/89865d4375634fc484f3a24b7fe65665
package main
/**
*
* @param A int整型一维数组
* @param B int整型一维数组
* @return void
*/
func merge( A []int , m int, B []int, n int ) {
i := 0
j := 0
res := make([]int, 0, len(A)+len(B))
for i < m && j < n {
if A[i] <= B[j] {
res = append(res, A[i])
i++
} else {
res = append(res, B[j])
j++
}
}
if i < m {
for k := i; k < m; k++ {
res = append(res, A[k])
}
}
if j < n {
for k := j; k < n; k++ {
res = append(res, B[k])
}
}
for k,_ := range A {
A[k] = res[k]
}
}