{"id":2045,"date":"2023-09-29T11:32:49","date_gmt":"2023-09-29T03:32:49","guid":{"rendered":"https:\/\/white-night.club\/?p=2045"},"modified":"2023-11-09T11:07:52","modified_gmt":"2023-11-09T03:07:52","slug":"ad19","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/09\/29\/ad19\/","title":{"rendered":"88.\u00a0\u5408\u5e76\u4e24\u4e2a\u6709\u5e8f\u6570\u7ec4"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e24\u4e2a\u6309&nbsp;<strong>\u975e\u9012\u51cf\u987a\u5e8f<\/strong>&nbsp;\u6392\u5217\u7684\u6574\u6570\u6570\u7ec4&nbsp;<code>nums1<\/code><em>&nbsp;<\/em>\u548c&nbsp;<code>nums2<\/code>\uff0c\u53e6\u6709\u4e24\u4e2a\u6574\u6570&nbsp;<code>m<\/code>&nbsp;\u548c&nbsp;<code>n<\/code>&nbsp;\uff0c\u5206\u522b\u8868\u793a&nbsp;<code>nums1<\/code>&nbsp;\u548c&nbsp;<code>nums2<\/code>&nbsp;\u4e2d\u7684\u5143\u7d20\u6570\u76ee\u3002<\/p>\n\n\n\n<p>\u8bf7\u4f60&nbsp;<strong>\u5408\u5e76<\/strong>&nbsp;<code>nums2<\/code><em>&nbsp;<\/em>\u5230&nbsp;<code>nums1<\/code>&nbsp;\u4e2d\uff0c\u4f7f\u5408\u5e76\u540e\u7684\u6570\u7ec4\u540c\u6837\u6309&nbsp;<strong>\u975e\u9012\u51cf\u987a\u5e8f<\/strong>&nbsp;\u6392\u5217\u3002<\/p>\n\n\n\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u6700\u7ec8\uff0c\u5408\u5e76\u540e\u6570\u7ec4\u4e0d\u5e94\u7531\u51fd\u6570\u8fd4\u56de\uff0c\u800c\u662f\u5b58\u50a8\u5728\u6570\u7ec4&nbsp;<code>nums1<\/code>&nbsp;\u4e2d\u3002\u4e3a\u4e86\u5e94\u5bf9\u8fd9\u79cd\u60c5\u51b5\uff0c<code>nums1<\/code>&nbsp;\u7684\u521d\u59cb\u957f\u5ea6\u4e3a&nbsp;<code>m + n<\/code>\uff0c\u5176\u4e2d\u524d&nbsp;<code>m<\/code>&nbsp;\u4e2a\u5143\u7d20\u8868\u793a\u5e94\u5408\u5e76\u7684\u5143\u7d20\uff0c\u540e&nbsp;<code>n<\/code>&nbsp;\u4e2a\u5143\u7d20\u4e3a&nbsp;<code>0<\/code>&nbsp;\uff0c\u5e94\u5ffd\u7565\u3002<code>nums2<\/code>&nbsp;\u7684\u957f\u5ea6\u4e3a&nbsp;<code>n<\/code>&nbsp;\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3\n<strong>\u8f93\u51fa\uff1a<\/strong>[1,2,2,3,5,6]\n<strong>\u89e3\u91ca\uff1a<\/strong>\u9700\u8981\u5408\u5e76 [1,2,3] \u548c [2,5,6] \u3002\n\u5408\u5e76\u7ed3\u679c\u662f [<em><strong>1<\/strong><\/em>,<em><strong>2<\/strong><\/em>,2,<em><strong>3<\/strong><\/em>,5,6] \uff0c\u5176\u4e2d\u659c\u4f53\u52a0\u7c97\u6807\u6ce8\u7684\u4e3a nums1 \u4e2d\u7684\u5143\u7d20\u3002\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>nums1 = [1], m = 1, nums2 = [], n = 0\n<strong>\u8f93\u51fa\uff1a<\/strong>[1]\n<strong>\u89e3\u91ca\uff1a<\/strong>\u9700\u8981\u5408\u5e76 [1] \u548c [] \u3002\n\u5408\u5e76\u7ed3\u679c\u662f [1] \u3002\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>nums1 = [0], m = 0, nums2 = [1], n = 1\n<strong>\u8f93\u51fa\uff1a<\/strong>[1]\n<strong>\u89e3\u91ca\uff1a<\/strong>\u9700\u8981\u5408\u5e76\u7684\u6570\u7ec4\u662f [] \u548c [1] \u3002\n\u5408\u5e76\u7ed3\u679c\u662f [1] \u3002\n\u6ce8\u610f\uff0c\u56e0\u4e3a m = 0 \uff0c\u6240\u4ee5 nums1 \u4e2d\u6ca1\u6709\u5143\u7d20\u3002nums1 \u4e2d\u4ec5\u5b58\u7684 0 \u4ec5\u4ec5\u662f\u4e3a\u4e86\u786e\u4fdd\u5408\u5e76\u7ed3\u679c\u53ef\u4ee5\u987a\u5229\u5b58\u653e\u5230 nums1 \u4e2d\u3002\n<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul>\n<li><code>nums1.length == m + n<\/code><\/li>\n\n\n\n<li><code>nums2.length == n<\/code><\/li>\n\n\n\n<li><code>0 &lt;= m, n &lt;= 200<\/code><\/li>\n\n\n\n<li><code>1 &lt;= m + n &lt;= 200<\/code><\/li>\n\n\n\n<li><code>-10<sup>9<\/sup> &lt;= nums1[i], nums2[j] &lt;= 10<sup>9<\/sup><\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>\u8fdb\u9636\uff1a<\/strong>\u4f60\u53ef\u4ee5\u8bbe\u8ba1\u5b9e\u73b0\u4e00\u4e2a\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a&nbsp;<code>O(m + n)<\/code>&nbsp;\u7684\u7b97\u6cd5\u89e3\u51b3\u6b64\u95ee\u9898\u5417\uff1f<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p>\u89e3\u9898\u601d\u8def<\/p>\n<cite><em>\u53ea\u4f1a\u66b4\u529b\u7684\u4eba\u53ea\u80fd\u5ea6\u8fc7\u76f8\u5bf9\u5931\u8d25\u7684\u4e00\u751f<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u561b\uff0c\u4e00\u5f00\u59cb\u60f3\u5230\u8981\u7528\u53cc\u6307\u9488\u4e86\u3002\u4f46\u662f\u6572\u5b8c\u63d0\u4ea4\u53ea\u8fc7\u4e86\u4e00\u534a\uff0c\u53c8\u4e0d\u60f3\u518d\u52a0if\u8bed\u53e5\uff0c\u5c31\u76f4\u63a5\u66b4\u529b\u89e3\u51b3\u4e86\u3002\u56e0\u4e3avector\u7a7a\u95f4\u662f\u591f\u7684\uff0c\u6240\u4ee5\u76f4\u63a5\u628a\u7b2c\u4e8c\u4e2a\u6570\u7ec4\u8986\u76d6\u5230\u7b2c\u4e00\u4e2a\u6570\u7ec4\u540e\u9762\uff0c\u7136\u540e\u76f4\u63a5\u7528C\u81ea\u5e26\u7684sort\u89e3\u51b3\u3002<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>\u4ee3\u7801\uff08\u66b4\u529b\uff09<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>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=m,j=0;i&lt;m+n;i++,j++)\n        {\n            nums1&#91;i]=nums2&#91;j];\n        }\n        sort(nums1.begin(),nums1.end());\n    }\n};<\/code><\/pre>\n\n\n\n<p>\u4f46\u8fd9\u4e48\u505a\u5237\u9898\u5c31\u6ca1\u5565\u610f\u4e49\u4e86\uff0c\u672c\u8eab\u5c31\u662f\u6765\u7ec3\u7b97\u6cd5\u7684\u561b\u3002<\/p>\n\n\n\n<p>\u53cc\u6307\u9488\uff0c\u542f\u52a8\uff01\u4e0d\u8fc7\u8bf4\u771f\u7684\uff0c\u6211\u53ea\u60f3\u5230\u4e86\u53cc\u6307\u9488\u3002\u6ca1\u60f3\u5230\u8fd8\u53ef\u4ee5\u9006\u5411\u53cc\u6307\u9488\u3002<\/p>\n\n\n\n<p>\u4e0d\u8fc7\u53eb\u56db\u6307\u9488\u53ef\u80fd\u4f1a\u66f4\u5bb9\u6613\u7406\u89e3\uff1f\u5148\u6709\u4e24\u4e2a\u4e0b\u6807\u5206\u522b\u6307\u5411\u4e24\u4e2anums\u6570\u7ec4\u771f\u5b9e\u7684\u201d\u5c3e\u90e8\u201c\uff08nums1\u6570\u7ec4\u603b\u7a7a\u95f4\u662fm+n-1\uff0c\u4f46\u5b9e\u9645\u6709\u610f\u4e49\u7684\u53d6\u503c\u53ea\u5230\u4e0b\u6807m-1\uff09\u3002\u518d\u7528\u4e00\u4e2atail\u6307\u9488\u6307\u5411nums1\u7684\u5c3e\u90e8\uff0c\u8868\u793a\u201c\u8bf7\u7528\u6bd4\u8fd9\u4e2a\u503c\u66f4\u5927\u7684\u503c\u6765\u8fdb\u884c\u66ff\u4ee3\uff08<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u4e0d\u662f\u66ff\u6362\uff01<\/mark>\u5176\u5b9e\u5c31\u662f\u628a\u5927\u7684\u503c\u5f80nums1\u540e\u9762\u653e\u3002nums1\u524d\u9762\u600e\u4e48\u6837\u662f\u4e0d\u7528\u7ba1\u7684\uff09\u201d\u3002\u6700\u540e\u518d\u7528\u4e00\u4e2a\u6e38\u6807\u6307\u9488\u8868\u793a\u201c\u66ff\u6362tail\u6240\u6307\u4f4d\u7f6e\u201c\u7684\u503c\u3002<\/p>\n\n\n\n<p>\u5f53\u4e24\u4e2a\u4e0b\u6807\u5176\u4e2d\u4e00\u4e2a\u7684\u503c\u53d8\u4e3a-1\u65f6\uff0c\u8bf4\u660e\u5176\u5bf9\u5e94\u7684\u6570\u7ec4\u5df2\u7ecf\u904d\u5386\u5b8c\u4e86\u3002\u6bd4\u5982\u5f53p1=-1\u65f6\uff0c\u8868\u793anums1\u5df2\u7ecf\u6ca1\u6709\u8fd8\u672a\u6392\u5e8f\u7684\u5143\u7d20\u4e86\uff0c\u4e14nums2\u5269\u4f59\u7684\u503c\uff08\u5982\u679c\u6709\u7684\u8bdd\uff09\u90fd\u4e00\u5b9a\u8981\u6bd4\u6b64\u65f6tail\u6307\u9488\u6240\u6307\u5411\u7684\u503c\u5c0f\u3002\u90a3\u76f4\u63a5\u5f80\u524d\u6392\u5c31\u884c\u4e86\u3002<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>\u4ee3\u7801\uff08\u9006\u5411\u53cc\u6307\u9488\uff09<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\npublic:\n    void merge(vector&lt;int&gt;&amp; nums1, int m, vector&lt;int&gt;&amp; nums2, int n) {\n        int p1=m-1,p2=n-1;\n        int tail=m+n-1;\n        int cur;\n        while(p1&gt;=0||p2&gt;=0)\n        {\n            if(p1==-1)\n            {\n                cur=nums2&#91;p2--];\n            }else if(p2==-1)\n            {\n                cur=nums1&#91;p1--];\n            }else if(nums1&#91;p1]&gt;nums2&#91;p2])\n            {\n                cur=nums1&#91;p1--];\n            }else\n            {\n                cur=nums2&#91;p2--];\n            }\n            nums1&#91;tail--]=cur;\n        }\n    }\n};<\/code><\/pre>\n\n\n\n<p>\u55ef&#8230;\u53cc\u6307\u9488\u6211\u80fd\u60f3\u5230\uff0c\u4f46\u9006\u5411\u53cc\u6307\u9488\u771f\u7684\u662f\u7b2c\u4e00\u6b21\u89c1\u3002\u8fd8\u662f\u5f97\u505a\u5230\u4e3e\u4e00\u53cd\u4e09\u624d\u884c\u554a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Leetcode\u9898\u76ee\uff1a\u66b4\u529b&#038;&#038;\u9006\u5411\u53cc\u6307\u9488<\/p>\n","protected":false},"author":1,"featured_media":2827,"comment_status":"open","ping_status":"","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[42,25],"tags":[38],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/2045"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=2045"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/2045\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/2827"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=2045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=2045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=2045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}