LeetCode 88 Merge Sorted Array (Merge sort Array )(*)

Source: Internet
Author: User

LeetCode 88 Merge Sorted Array (Merge sort Array )(*)
Translation

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 to form a sorted array. Annotation: you can assume that nums1 has enough space (space greater than or equal to m + n) to store additional elements from nums2. The initial space of nums1 and nums2 is m and n.
Original
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.
Analysis

One idea is to set another array, add the data in nums1 and nums2 to the new array one by one, and assign the entire array to nums1, just like cheating, the meaning of the question is to be included in nums1, so that nums1 does not change.

So, keep thinking about new methods ......

Let's take a look at the following relationships:

1. m and n indicate the number of initialized elements in nums1 and nums2, rather than the space size of nums1 and nums2. That is to say, the space in nums1 is large enough, however, the m space sets the value of this setting. In this question, we call it the valid number 2. From 1, we can conclude that the total valid number after merging is m + n3, since it all starts from 0, the index of the last element of nums1 and nums2 is expressed as -1 and n-1, respectively. The index of the last element of the merged nums1 should be m + N-14, we merge nums2 into nums1, so the overall cycle can start from nums2 5. Next I will explain why the internal loop starts from the end of nums1: because this is a vector Array rather than a linked list, they are indexed, and the index is from front to back (from 0 to m-1). If you add a number in front of the array, all subsequent elements need to be moved back. If a number is added to the rear, the previous element does not need to be moved. As for why don't you worry about the rear space problem, because the question says that the space for nums1 is large enough.

Forgive me for not having to draw a vertical line for the array to distinguish each grid. I believe everyone understands it. I have tried my best, haha ......

View code ......

Code
class Solution {public:    void merge(vector
  
    &nums1, int m, vector
   
     &nums2, int n) {        int i = m - 1, j = n - 1, position = m + n - 1;        while (j >= 0) {            nums1[position--] = i >= 0 && nums1[i] > nums2[j] ? nums1[i--] : nums2[j--];        }    }};
   
  

Related Article

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.