Question:
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 to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.
public class Solution { public void merge(int A[], int m, int B[], int n) { int lena = m-1; int lenb = n-1; int lenres = m+n-1; while(lena>=0&&lenb>=0){ if (A[lena]>=B[lenb]) { A[lenres--] = A[lena--]; }else { A[lenres--] = B[lenb--]; } } while(lena>=0){ A[lenres--] = A[lena--]; } while(lenb>=0){ A[lenres--] = B[lenb--]; } } }