Advertisement
sweet1cris

Untitled

Jan 9th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1. class Solution {
  2.     /**
  3.      * @param A: sorted integer array A which has m elements,
  4.      *           but size of A is m+n
  5.      * @param B: sorted integer array B which has n elements
  6.      * @return: void
  7.      */
  8.     public void mergeSortedArray(int[] A, int m, int[] B, int n) {
  9.         int i = m-1, j = n-1, index = m + n - 1;
  10.         while (i >= 0 && j >= 0) {
  11.             if (A[i] > B[j]) {
  12.                 A[index--] = A[i--];
  13.             } else {
  14.                 A[index--] = B[j--];
  15.             }
  16.         }
  17.         while (i >= 0) {
  18.             A[index--] = A[i--];
  19.         }
  20.         while (j >= 0) {
  21.             A[index--] = B[j--];
  22.         }
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement