lintcode-64-合并排序数组 II

64-合并排序数组 II

合并两个排序的整数数组A和B变成一个新的数组。

注意事项

你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B中的元素。

样例

给出 A = [1, 2, 3, empty, empty], B = [4, 5]
合并之后 A 将变成 [1,2,3,4,5]

标签

数组 排序数组 脸书

思路

从后向前遍历数组

code

class Solution {
public:
    /**
     * @param A: sorted integer array A which has m elements, 
     *           but size of A is m+n
     * @param B: sorted integer array B which has n elements
     * @return: void
     */
    void mergeSortedArray(int A[], int m, int B[], int n) {
        // write your code here
        int size = m + n, i = 0, a = m-1, b = n-1;
        for(i=size-1; i>=0; i--){
            if(A[a] <= B[b]) {
                A[i] = B[b];
                b--;
            }
            else {
                A[i] = A[a];
                a--;
            }
        }
    }
};
原文地址:https://www.cnblogs.com/libaoquan/p/7118285.html