题解 | #合并两个有序的数组#
合并两个有序的数组
http://www.nowcoder.com/practice/89865d4375634fc484f3a24b7fe65665
public class Solution { public void merge(int A[], int m, int B[], int n) { // 二分查找然后插入 for(int i=0;i<n;i++){ int val = B[i]; // 查找val在A中应该存放的位置 int low = 0; int high = m+i-1; while(low<=high){ int mid = (low+high)/2; if(A[mid]>val){ high = mid-1; } else { low = mid + 1; } } //此时low>high for(int j=m+i-1;j>high;j--){ A[j+1] = A[j]; } A[high+1] = val; } } }