这个问题,我先用了第一种方法,自己测试过了,但是leetcode说有下面错误。
但是我的机器上没有错误。
Submission Result: Runtime Error
Last executed input: [1,2,4,5,6], [3]
用了另一种方法,大的元素,从A[m+n-1]排起过了到A[0],过了。
但是这个小的元素,从A[0]到A[m+n-1]排起,不过
但是输出是一样的不知道为啥。郁闷啊,大家帮忙看看
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 and n respectively.
void merge(int A[], int m, int B[], int n) {
if (n==0) return;//If n=0,do nothing
if (m==0){
for (int i=0;iA[i]=B[i];
return;
}//If m=0, copy B to A
for (int i=0;iA[m+i]=A[i];
}//Move A[0]...A[m-1] elements to A[m]..A[2m-1]
int a=m,b=0;//set a pointer to the start of A, m, and B to 0
//copy A and B in order to A,from 0 to m+n-1
for (int i=0;iif (a>2*m-1) A[i]=B[b++];
else if (b>n-1) A[i]=A[a++];
else if (A[a]>B[b]) A[i]=B[b++];
else A[i]=A[a++];
}
}