从后向前遍历
1 public class Solution { 2 public void merge(int A[], int m, int B[], int n) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int indexA = m - 1; 6 int indexB = n - 1; 7 int k = m + n - 1; 8 while(indexA >= 0 && indexB >= 0) 9 { 10 if(A[indexA] >= B[indexB]) 11 { 12 A[k] = A[indexA]; 13 indexA--; 14 } 15 else 16 { 17 A[k] = B[indexB]; 18 indexB--; 19 } 20 k--; 21 } 22 while(indexB >= 0) 23 { 24 A[k] = B[indexB]; 25 indexB--; 26 k--; 27 } 28 } 29 }