NC22 合并两个有序的数组

https://www.nowcoder.com/practice/89865d4375634fc484f3a24b7fe65665?tpId=117&rp=1&ru=%2Fexam%2Fcompany&qru=%2Fexam%2Fcompany&sourceUrl=%2Fexam%2Fcompany&difficulty=&judgeStatus=&tags=&title=&gioEnter=menu

代码


#include <type_traits>
class Solution {
public:
    void merge(int A[], int m, int B[], int n) {
        if(!n) return;
        int a = m-1;
        int b = n-1;
        int index = m + n - 1;
        while(a >= 0 && b >= 0){
            if(A[a] >= B[b]){
                A[index--] = A[a--];
            }else{
                A[index--] = B[b--];
            }
        }
        //如果B中存在比A中最小的还要小的数 则不会被全部拷贝
        while(b >= 0){
            A[index--] = B[b--];
        }
        
        return;
    }
};
posted @ 2024-01-11 14:00  xiazichengxi  阅读(13)  评论(0)    收藏  举报