合并两个有序数组;
合并两个有序的数组
http://www.nowcoder.com/questionTerminal/89865d4375634fc484f3a24b7fe65665
采用归并的思路解决这个问题
public class Solution {
public void merge(int A[], int m, int B[], int n) {
int[] result=new int[m+n];
int t=0;
int i=0,j=0;
while(i<m&&j<n){
if(A[i]<=B[j]){//两边有序数组相互比较得出小的元素依次存放
result[t++]=A[i++];
}else{
result[t++]=B[j++];
}
}
//将剩余元素依次存放在result数组中
while(i<m){//左边数组A如果还有剩余元素,全部存放在result数组中
result[t++]=A[i++];
}
while(j<n){//右边数组B如果还有剩余元素,全部存放在result数组中
result[t++]=B[j++];
}
for(int sum=0;sum<result.length;sum++){//最后通过遍历将result数组元素存放到A数组中
A[sum]=result[sum];
}
}
}
查看23道真题和解析
