个人博客:https://luxialan.com

Merge Sorted Array 分类: Leetcode(排序) 2015-04-08 21:52 24人阅读 评论(0) 收藏

Merge Sorted Array

Given two sorted integer arrays A and B, merge B into A as one sorted array.

Note:
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 mand n respectively.


class Solution {
public:
    void merge(int A[], int m, int B[], int n) {
        int ia = m-1 , ib = n-1 , ic = m+n-1;
        while ( ia >= 0 && ib >= 0 ) {
            if( A[ia] >= B[ib]) A[ic--] = A[ia--];
            else A[ic--] = B[ib--];
        }
        while(ib >=0) {
            A[ic--] = B[ib--];
        }
    };
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-04-08 21:52  luxialan  阅读(138)  评论(0)    收藏  举报