LeetCode: Merge Sorted Array


Problem:
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
2つの秩序配列が結合されます.
class Solution {
public:
    void merge(int A[], int m, int B[], int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int idx = m + n - 1;
        int i = m - 1;
        int j = n - 1;
        while(i >= 0 && j >= 0)
        {
            if (A[i] > B[j])
            {
                A[idx--] = A[i--];
            }
            else if (A[i] < B[j])
            {
                A[idx--] = B[j--];
            }
            else
            {
                A[idx--] = A[i--];
                A[idx--] = B[j--];
            }
        }
        while (j >= 0)
        {
            A[idx--] = B[j--];
        }
    }
};