解法一:
import java.util.*;
public class Solution {
public void merge(int A[], int m, int B[], int n) {
int i = 0,j = 0,k = 0;//定义指针
int [] C = new int [m + n];
while(i < m && j < n){
if(A[i]<B[j]){
C[k++]=A[i++];
}else{
C[k++]=B[j++];
}
}
while(i < m){
C[k++]=A[i++];
}
while(j < n){
C[k++]=B[j++];
}
for(int u = 0;u < m+n;u++){
A[u]=C[u];
}
}
}
解法二:
import java.util.*;
public class Solution {
public void merge(int A[], int m, int B[], int n) {
int i = m-1,j = n-1,k = m+n-1;//定义指针
//从最大的开始遍历
while(i >= 0&& j>=0){
if(A[i]>B[j]){
A[k--]=A[i--];
}else{
A[k--]=B[j--];
}
}
while(i >= 0){
A[k--] = A[i--];
}
while(j >= 0){
A[k--] = B[j--];
}
}
}