Merge Sorted Array

来源:互联网 发布:舍利时时彩源码 编辑:程序博客网 时间:2024/05/18 02:42
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. The 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){if(A[ia] > B[ib])A[icur--] = A[ia--];elseA[icur--] = B[ib--];}while(ib >= 0)A[icur--] = B[ib--];}};



0 0
原创粉丝点击