Merge Two Sorted Arrays
Question
Example
Input: A = [1,2,3,4], B=[2,4,5,6]
Return: [1,2,2,3,4,4,5,6]Analysis
Easy To Get Bug
public int[] mergeSortedArray(int[] A, int[] B) {
int[] R = new int[A.length + B.length]; //result
int x = 0, y = 0;
while (x + y < R.length) {
if (y >= B.length || (x < A.length && A[x] <= B[y])) {
R[x+y] = A[x++];
} else {
R[x+y] = B[y++];
}
}
return R;
}Hard to Get Bug
Merge Sorted Array (LC.88)
Example
Analysis
Code
Recap
Last updated