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.
给定两个有序的数组A和B,把两个数组合并成一个数组。
class Solution { public: void merge(int A[], int m, int B[], int n) { int pa=m-1; int pb=n-1; int p=n+m-1; while(pa>=0 && pb>=0){ if(A[pa]>B[pb]){ A[p--]=A[pa--]; } else{ A[p--]=B[pb--]; } } while(pb>=0){ A[p--]=B[pb--]; } } };
LeetCode: Merge Sorted Array [088],布布扣,bubuko.com
LeetCode: Merge Sorted Array [088]
原文地址:http://blog.csdn.net/harryhuang1990/article/details/27796975