{"id":1883,"date":"2016-03-18T14:23:15","date_gmt":"2016-03-18T06:23:15","guid":{"rendered":"http:\/\/boweihe.me\/?p=1883"},"modified":"2016-03-18T14:23:15","modified_gmt":"2016-03-18T06:23:15","slug":"leetcode-88-merge-sorted-array","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1883","title":{"rendered":"LeetCode 88. Merge Sorted Array"},"content":{"rendered":"<p>Given two sorted integer arrays <i>nums1<\/i> and <i>nums2<\/i>, merge <i>nums2<\/i> into <i>nums1<\/i> as one sorted array.<br \/>\n<b>Note:<\/b><br \/>\nYou may assume that <i>nums1<\/i> has enough space (size that is greater or equal to <i>m<\/i> + <i>n<\/i>) to hold additional elements from <i>nums2<\/i>. The number of elements initialized in <i>nums1<\/i> and<i>nums2<\/i> are <i>m<\/i> and <i>n<\/i> respectively.<br \/>\n<strong>\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre class=\"lang:c++ decode:true  \">class Solution {\npublic:\n    void merge(vector&lt;int&gt;&amp; nums1, int m, vector&lt;int&gt;&amp; nums2, int n) {\n        for(int i=0; i&lt;n; i++) {\n            \/\/ \/!\\ Mind that m might be less than the actual size of nums1\n            if(nums1.size() &lt; m + i + 1)\n                nums1.push_back(-1);\n        }\n        int pos_1 = m-1;\n        int pos_2 = n-1;\n        int end = m + n - 1;\n        while(pos_1 &gt;= 0 &amp;&amp; pos_2 &gt;= 0) {\n            if(nums1[pos_1] &gt; nums2[pos_2]) {\n                nums1[end] = nums1[pos_1];\n                pos_1--;\n            } else {\n                nums1[end] = nums2[pos_2];\n                pos_2--;\n            }\n            end--;\n        }\n        while(pos_2 &gt;= 0){\n            nums1[end--] = nums2[pos_2--];\n        }\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 andnums2 are m and n respectively. \u4ee3\u7801\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66],"class_list":["post-1883","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1883","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1883"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1883\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}