Problem
Given two sorted integer arrays A and B, merge B into A as one sorted array. You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.Algorithm
If merge two arrays from left to right, we may find some difficulties to relocate elements in A. For example, if A = [3,4, ] B = [1].
1. Relocate m elements in A to range A[m-1, m+n-1], then merge two arrays from left to right.
For example, after relocation, A = [ ,3,4] B = [1], then merge.
2. Merge two arrays in descending order, since array A is empty on the right side.
No comments:
Post a Comment