【题解】【数组】【Leetcode】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. The number of elements initialized in A and B are m and n respectively.

思路:

归并的一部分,比Merge Two Sorted Lists还要简单

代码:

【题解】【数组】【Leetcode】Merge Sorted Array
 1 void merge(int A[], int m, int B[], int n) {
 2     int i = m-1;
 3     int j = n-1;
 4     int t = m+n-1;//t不能定义在for循环内,否则最后无法访问
 5     while(t>=0 && i>=0 && j>=0){
 6         if(A[i] > B[j]) A[t--] = A[i--];
 7         else A[t--] = B[j--];
 8     }
 9     while(j >= 0) A[t--] = B[j--];
10 }
【题解】【数组】【Leetcode】Merge Sorted Array

【题解】【数组】【Leetcode】Merge Sorted Array

上一篇:关于开发的点点滴滴


下一篇:Cocos2d-x游戏开发之旅