Merge Two Sorted Arrays

class Solution {
    /**
     * @param A and B: sorted integer array A and B.
     * @return: A new sorted integer array
     */
    public int[] mergeSortedArray(int[] A, int[] B) {
        // Write your code here
        int n = A.length + B.length;
        int[] rst = new int[n];
        int a = 0;
        int b = 0;
        int i = 0;
        while (a < A.length && b < B.length) {
            if (A[a] < B[b]) {
                rst[i++] = A[a++];
            } else {
                rst[i++] = B[b++];
            }
        }
        while (a < A.length) {
            rst[i++] = A[a++];
        }
        while (b < B.length) {
            rst[i++] = B[b++];
        }
        return rst;
    }
}

 

posted on 2017-05-31 09:07  codingEskimo  阅读(107)  评论(0)    收藏  举报

导航