-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCCI1001-SortedMerge.go
More file actions
49 lines (42 loc) · 1.02 KB
/
LCCI1001-SortedMerge.go
File metadata and controls
49 lines (42 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
// 面试题 10.01. Sorted Merge LCCI
// You are given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B.
// Write a method to merge B into A in sorted order.
// Initially the number of elements in A and B are m and n respectively.
// Example:
// Input:
// A = [1,2,3,0,0,0], m = 3
// B = [2,5,6], n = 3
// Output: [1,2,2,3,5,6]
// Note:
// A.length == n + m
import "fmt"
func merge(A []int, m int, B []int, n int) {
i, j, k := m - 1, n - 1, m + n - 1
for i >= 0 && j >= 0 {
if A[i] > B[j] {
A[k] = A[i]
i--
} else {
A[k] = B[j]
j--
}
k--
}
if j >= 0 {
copy(A[:k+1], B[:j+1])
}
}
func main() {
// Example:
// Input:
// A = [1,2,3,0,0,0], m = 3
// B = [2,5,6], n = 3
// Output: [1,2,2,3,5,6]
A := []int{1,2,3,0,0,0}
B := []int{2,5,6}
fmt.Println("A: ", A)
fmt.Println("B: ", B)
merge(A, 3, B, 3)
fmt.Println("A: ", A)
}