Leetcode [88] (Java): Merge Sorted Array (merge sorted array)

Source: Internet
Author: User

title : Merging sorted arrays

Difficulty : Easy

topic content :

Given sorted integer Arrays nums1 and nums2, merge nums2 into nums1 as one sorted Array.

translation :

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as a sorted array.

Attention:

The number of elements initialized in Nums1 and NUMS2 are M and N, respectively.

NUMS1 has enough space (greater than or equal to m+n) to accommodate the extra elements in the NUMS2.

My idea : There's a little bit of merging here and in the merge sort, but the difference is that the length of the NUMS1 array is the length after the merge.

In this way, merging in the traditional merge order is not possible because the number of NUMS1 arrays may be overwritten during the merge process (eg. "4,5,0,0" "" ")

Unless you create a new temporary array, merge the two into this array, and then copy the values of this array all to NUMS1, then the length of the nums1 is meaningless.

So you can consider the second method of merge sort ——— insert sort. Principle no longer repeat. Take the NUMS2 directly as the number after the NUMS1.

Three pointers: I represents the position of the NUMS2 at this time (starting at 0), J represents the position of nums1 at this time (starting from m-1), and K represents the position of nums1 in the inner loop.

My Code :

1      Public voidMergeint[] nums1,intMint[] Nums2,intN) {2         intj = M-1;3          for(inti = 0; I < n; i++) {4             if(j = =-1 | | nums2[i] >=Nums1[j]) {5                  while(I <N) {6NUMS1[++J] = nums2[i++];7                 }8                 return;9}Else {Ten                 intK =J; One                  for(k >-1 && nums1[k] > nums2[i]; k--) { ANUMS1[K+1] =Nums1[k]; -                 } -NUMS1[K+1] =Nums2[i]; theJ + +; -             } -         } -}

my complexity : O (n*m) space complexity O (1)

problems in the encoding process :

1, the 4th line, when M==0, NUMS1 also has a 0, at this time if not to judge, will be 0 as an element directly into the merge sort, then will cross, so join j = = 1 | |

eg. "0" m=0 "1" n=1.

2, the 15th line, forgot to put a, nums1 the pointer needs to move to the right a--j++

Answer code :

1  Public voidMergeintA[],intMintB[],intN) {2     inti = m-1, j = n-1, k = m+n-1;3      while(I>-1 && j>-1)4a[k--] = A[i] > b[j]? a[i--]: b[j--];5      while(j>-1)        6a[k--] = b[j--];7}

Answer Ideas :

Since the trip will be covered, but the back is all 0, then from the back forward will not be covered.

(even if the extreme situation, a is all less than B, the back of the 0 is just enough B, there will be no coverage)

1, when I and J are not exhausted, from the back to the front, who is large who entered, and the corresponding pointer consumption 1;

2, if J has not consumed, then put the rest of b into a; (at this point if a did not consume and do not need to tube, because it is already in a in order in the sequence)

Leetcode [88] (Java): Merge Sorted Array (merge sorted array)

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.