[leetcode 88] Merge Sorted Array

来源:互联网 发布:能直接利用geo数据库数 编辑:程序博客网 时间:2024/05/16 08:21

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 m andn respectively.

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


0 0
原创粉丝点击