您的位置:首页 > 其它

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

2014-02-01 13:06 495 查看
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还要简单

代码:

void merge(int A[], int m, int B[], int n) {
int i = m-1;
int j = n-1;
int t = m+n-1;//t不能定义在for循环内,否则最后无法访问
while(t>=0 && i>=0 && j>=0){
if(A[i] > B[j]) A[t--] = A[i--];
else A[t--] = B[j--];
}
while(j >= 0) A[t--] = B[j--];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: