Leetcode Merge Sorted Array (sort)

Source: Internet
Author: User

"Description of the problem"

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

Note:
Assume that nums1 have enough space (size that's greater or equal to m + n) to hold add Itional elements from nums2. The number of elements initialized in nums1and nums2 is m and n respectively.

1. "Basic Knowledge"

The idea of merging sort

There are two main types of sorting, one cutting the sequence to be sorted first. Decrease the length of the subsequence to 1, and then. Merge again. The second is to sort the 22 groupings to sort and then merge.

The idea of heap sequencing

See: Binary heap sequencing for data structure basics

Detailed Address: http://blog.csdn.net/u013630349/article/details/46906969

2. "Cock Wire Source Code"

Failed to give a complete implementation!

Class Solution {public:    void merge (vector<int>& nums1, int m, vector<int>& nums2, int n) {        if (Nums1.empty ()) {nums1 = Nums2;return;} if (Nums2.empty ()) return; Qkst (NUMS1); Qkst (NUMS2);vector<int> myvec;int k=0,j=0;for (int i=0;i<nums2.size () +nums1.size (); i++) {//One side is exhausted what to do if (K>=nums2.size ()) {fornums1 = Myvec;return;} if (J>=nums1.size ()) {fornums1 = Myvec;return;} if (Nums1[j]>=nums2[k]) {myvec.push_back (nums2[k]); k++;} Else{myvec.push_back (Nums1[j]); ++j;}} NUMS1 = Myvec;return;    } int pivot (int low,int high,vector<int> &vec) {return 0;} void Qkst (Vector<int> &vec) {}};


3. "AC Source code"

Class Solution {public:    void merge (vector<int>& nums1, int m, vector<int>& nums2, int n) {int ia = M-1, IB = n-1, icur = m + n-1;while (ia >= 0 && IB >= 0) {nums1[icur--] = Nums1[ia] >= Nums2[ib]?

nums1[ia--]: nums2[ib--];} while (IB >= 0) {nums1[icur--] = nums2[ib--];}};



4. "Algorithmic thinking"

1) When inserting operation is encountered, it is best to solve the time complexity problem with space complexity;
2) Insert operation, in fact operation for tail operation than the first operation to be simple, direct.

3) Complications of simple problems are the least worthwhile. Goal-oriented, dry out, with the results to say something else!

4) Jam Place: A. examining; b. conceptual confusion; c. results-oriented unclear.

Leetcode Merge Sorted Array (sort)

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.