{"id":5205,"date":"2023-11-17T15:27:25","date_gmt":"2023-11-17T07:27:25","guid":{"rendered":"https:\/\/white-night.club\/?p=5205"},"modified":"2023-11-17T15:50:33","modified_gmt":"2023-11-17T07:50:33","slug":"ad153","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/11\/17\/ad153\/","title":{"rendered":"88.\u00a0\u5408\u5e76\u4e24\u4e2a\u6709\u5e8f\u6570\u7ec4"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e24\u4e2a\u6309\u00a0<strong>\u975e\u9012\u51cf\u987a\u5e8f<\/strong>\u00a0\u6392\u5217\u7684\u6574\u6570\u6570\u7ec4\u00a0<code>nums1<\/code><em>\u00a0<\/em>\u548c\u00a0<code>nums2<\/code>\uff0c\u53e6\u6709\u4e24\u4e2a\u6574\u6570\u00a0<code>m<\/code>\u00a0\u548c\u00a0<code>n<\/code>\u00a0\uff0c\u5206\u522b\u8868\u793a\u00a0<code>nums1<\/code>\u00a0\u548c\u00a0<code>nums2<\/code>\u00a0\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><strong>\u8f93\u5165\uff1a<\/strong>nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 <strong>\u8f93\u51fa\uff1a<\/strong>[1,2,2,3,5,6] <strong>\u89e3\u91ca\uff1a<\/strong>\u9700\u8981\u5408\u5e76 [1,2,3] \u548c [2,5,6] \u3002 \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<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><strong>\u8f93\u5165\uff1a<\/strong>nums1 = [1], m = 1, nums2 = [], n = 0 <strong>\u8f93\u51fa\uff1a<\/strong>[1] <strong>\u89e3\u91ca\uff1a<\/strong>\u9700\u8981\u5408\u5e76 [1] \u548c [] \u3002 \u5408\u5e76\u7ed3\u679c\u662f [1] \u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><strong>\u8f93\u5165\uff1a<\/strong>nums1 = [0], m = 0, nums2 = [1], n = 1 <strong>\u8f93\u51fa\uff1a<\/strong>[1] <strong>\u89e3\u91ca\uff1a<\/strong>\u9700\u8981\u5408\u5e76\u7684\u6570\u7ec4\u662f [] \u548c [1] \u3002 \u5408\u5e76\u7ed3\u679c\u662f [1] \u3002 \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<\/p>\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 has-link-color wp-elements-db7d1dcdabd125fe3cb1cd106e289972\">\n<p>\u89e3\u9898\u601d\u8def<\/p>\n<cite><em>\u53cc\u6307\u9488<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u5b9e\u9645\u4e0a\u8fd8\u662f\u53cc\u6307\u9488\u3002<\/p>\n\n\n\n<p>A\u6570\u7ec4\u7684\u5927\u5c0f\u9664\u4e86\u6709\u6548\u6570\u5b57\u8fd8\u6709\u75280\u586b\u5145\u7684\u65e0\u6548\u4f4d\uff0c0\u586b\u5145\u7684\u65e0\u6548\u4f4d\u6570\u521a\u597d\u5c31\u662fB\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u6240\u4ee5A\u6570\u7ec4\u521a\u521a\u597d\u53ef\u4ee5\u5bb9\u7eb3\u4e0bB\u6570\u7ec4\u7684\u5168\u90e8\u5143\u7d20\u3002<\/p>\n\n\n\n<p>\u4ece\u5934\u5f00\u59cb\u63d2\u5165\u6570\u636e\u7684\u8bdd\uff0c\u6bd4\u8f83\u9ebb\u70e6\uff0c\u56e0\u4e3a\u524d\u9762\u662fA\u7684\u6709\u6548\u6570\u5b57\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u6211\u4eec\u9009\u62e9\u4ece\u5c3e\u90e8\u5f00\u59cb\u8ba1\u7b97\uff0c\u8bbe\u4e00\u4e2atail\u6307\u9488\u6307\u5411\u5c3e\u90e8\u3002first\uff0csecond\u4e24\u4e2a\u6307\u9488\u5206\u522b\u6307\u5411\u4e24\u4e2a\u6570\u7ec4\u5404\u81ea\u7684\u6709\u6548\u4f4d\u672b\u5c3e\u3002<\/p>\n\n\n\n<p>\u56e0\u4e3a\u662f\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\uff0c\u6240\u4ee5\u54ea\u4e2a\u6570\u7ec4\u7684\u5927\uff0c\u5c31\u5f80\u540e\u5f80\u524d\u6392\uff0c\u540c\u65f6\u5c3e\u6307\u9488\u5f80\u524d\u79fb\u4e00\u4f4d\u3002\u6709\u70b9\u50cf\u9009\u62e9\u6392\u5e8f\u90a3\u79cd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func merge(nums1 &#91;]int, m int, nums2 &#91;]int, n int)  {\r\n    first:=m-1\r\n    second:=n-1\r\n    tail:=m+n-1\r\n    cur:=0\r\n    for first>=0||second>=0{\r\n        if first==-1{\r\n            cur=nums2&#91;second]\r\n            second--\r\n        }else if second==-1{\r\n            cur=nums1&#91;first]\r\n            first--\r\n        }else if nums1&#91;first]>nums2&#91;second]{\r\n            cur=nums1&#91;first]\r\n            first--\r\n        }else{\r\n            cur=nums2&#91;second]\r\n            second--\r\n        }\r\n        nums1&#91;tail]=cur\r\n        tail--\r\n    }\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Leetcode\u9898\u76ee\uff1a\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\/5205"}],"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=5205"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/5205\/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=5205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=5205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=5205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}