排序 -- 6.1 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 to hold additional elements from B. e number of
elements initialized in A and B are m and n respectively

**********************************************************************************************************/

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

A足夠大,從後往前排序,挨個放

參考資料:

LeetCode題解

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章