Merge Two Sorted Arrays
阿新 • • 發佈:2017-05-31
urn clas ray blog sorted and logs class cnblogs
class Solution { /** * @param A and B: sorted integer array A and B. * @return: A new sorted integer array */ public int[] mergeSortedArray(int[] A, int[] B) { // Write your code here int n = A.length + B.length; int[] rst = new int[n]; int a = 0;int b = 0; int i = 0; while (a < A.length && b < B.length) { if (A[a] < B[b]) { rst[i++] = A[a++]; } else { rst[i++] = B[b++]; } } while (a < A.length) { rst[i++] = A[a++]; }while (b < B.length) { rst[i++] = B[b++]; } return rst; } }
Merge Two Sorted Arrays