LeetCode: Merge Sorted Array

来源:互联网 发布:ipad如何看淘宝直播 编辑:程序博客网 时间:2024/05/16 14:34

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.

两个有序数组合并。

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--];        }    }};


原创粉丝点击