Merge Two Sorted Arrays
Merge two given sorted integer array A and B into a new sorted integer array.
Example
A=[1,2,3,4]
B=[2,4,5,6]
return [1,2,2,3,4,4,5,6]
1 class Solution { 2 /** 3 * @param A and B: sorted integer array A and B. 4 * @return: A new sorted integer array 5 * cnblogs.com/beiyeqingteng/ 6 */ 7 public int[] mergeSortedArray(int[] A, int[] B) { 8 int[] newArray = new int[A.length + B.length]; 9 10 int pointer = 0; 11 12 int pointerA = 0; 13 int pointerB = 0; 14 15 while (pointerA < A.length || pointerB < B.length) { 16 if (pointerB == B.length || pointerA < A.length && A[pointerA] <= B[pointerB]) { 17 newArray[pointer] = A[pointerA]; 18 pointerA++; 19 } else { 20 newArray[pointer] = B[pointerB]; 21 pointerB++; 22 } 23 pointer++; 24 } 25 26 return newArray; 27 } 28 }
转载请注明出处:cnblogs.com/beiyeqingteng/