Leetcode88--merge Sorted Array

Source: Internet
Author: User


Topic

Given sorted integer arrays A and B, merge B into a as one sorted array.

Note:you may assume, A has enough space (size, is greater or equal to M + N) to the hold additional elements from B. The number of elements initialized in A and B is M and N respectively.

void merge (int a[], int m, int b[], int n) {}

Main topic

Given two well-ordered int arrays A and B, merge B into a, and the merge remains orderly. Suppose that a has enough space.

Difficulty factor

Easy

Realize

void merge (int a[], int m, int b[], int n) {    int temp[m];    memcpy (temp, A, sizeof (int) * m);    bool desc = a[0] > a[m-1];    for (int k = 0, i = 0, j = 0; k < m + N; ++k) {&N bsp;       if (i >= m) {             a[k] = b[j++];       } else if (j >= N) {    & nbsp;       a[k] = temp[i++];       } else if (desc ) {            a[k] = Temp[i] >= B[j]? temp[i++]: B[j++];& nbsp;      } else {             A[K] = Temp[i] <= b[j]? temp[i++]: b[j++];       }   }}


This time I was delighted that a commit was passed. However, I found that the topic does not seem to need to consider the ascending or descending problem. I did not read the title has the default ascending test instructions, but the online are in ascending order to do, this point I am very puzzled, of course, as an interview question, it is assumed that ascending is also possible, after all, the descending way of writing similar, is ascending or descending, this is nothing good to examine. Anyway, my implementation, regardless of the lifting is not a problem.

Leetcode88--merge Sorted Array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.