https://oj.leetcode.com/problems/merge-sorted-array/
http://blog.csdn.net/linhuanmars/article/details/19712333
Public class solution { public void merge (int A[], Int m, int b[], int n) { // Solution A: // merge_noextraspace (a, m, b, n); // Solution B: merge_extraspace (a, m, b, n); } ///////////////////////////// // Solution A: Noextraspace // private void merge_noextraspace (int a[], int m, int b[], int n) { //&nBsp To not using extra space. copy existing a[0,m] to a[n, m+n] // Need to copy backwards for (int i = m - 1 ; i >= 0 ; i --) { A[i + n] = A[i]; } int i = n; int j = 0; int k = 0; while (i < m + n | | j < n) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSp { int a = i < m + n ? A[i] : Integer.MAX_VALUE; int b = j < n ? B[j] : integer.max_value; if (a < &NBSP;B) { A[k] = a; i ++; } else { a[k] = b; j ++; } k++; } } // // Solution B: ExtraSpace // private void merge_extraspace (int a[], int m, Int b[], int n) { int[] result = new int[m + n]; int i = 0; int j = 0; int k = 0; while (i < m | | j < n) { if (i == m) result[k ++] = B[j ++]; else if (j == n) result[k ++] = a[ i ++]; else if (A[ I]&NBSP;<&NBSP;B[J]) result[k ++] = a[i ++]; else result[k ++] = B[j ++]; } // copy result to a for (i = 0 ; i < result.length ; i ++) { a[i] = result[i]; } }}
[Leetcode]88 Merge Sorted Array