{"id":296,"date":"2025-06-26T14:27:00","date_gmt":"2025-06-26T06:27:00","guid":{"rendered":"http:\/\/getzs.online\/?p=296"},"modified":"2025-06-30T22:30:47","modified_gmt":"2025-06-30T14:30:47","slug":"%e5%90%88%e5%b9%b6%e4%b8%a4%e4%b8%aa%e6%9c%89%e5%ba%8f%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/hello.getzs.online\/index.php\/2025\/06\/26\/%e5%90%88%e5%b9%b6%e4%b8%a4%e4%b8%aa%e6%9c%89%e5%ba%8f%e6%95%b0%e7%bb%84\/","title":{"rendered":"\u5408\u5e76\u4e24\u4e2a\u6709\u5e8f\u6570\u7ec4"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/merge-sorted-array\/\">88. \u5408\u5e76\u4e24\u4e2a\u6709\u5e8f\u6570\u7ec4<\/a><\/p>\n\n\n\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 class=\"wp-block-list\">\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<pre class=\"wp-block-code\"><code>    public void merge(int&#91;] nums1, int m, int&#91;] nums2, int n) {\n\/\/        method1(nums1, m, nums2, n);\n        method2(nums1, m, nums2, n);\n    }\n\n    \/**\n     * \u7b80\u5355sort\n     * 1ms\n     * \u590d\u6742\u5ea6O(nlogn)\n     *\/\n    public void method1(int&#91;] nums1, int m, int&#91;] nums2, int n) {\n        if (n >= 0) {\n            System.arraycopy(nums2, 0, nums1, m, n);\n        }\n        Arrays.sort(nums1);\n    }\n\n    \/**\n     * \u9006\u5e8f\u53cc\u6307\u9488 -> \u907f\u514d\u5143\u7d20\u88ab\u8986\u76d6\n     * 0ms\n     * \u590d\u6742\u5ea6O(n+m)\n     *\/\n    public void method2(int&#91;] nums1, int m, int&#91;] nums2, int n) {\n        int i = m - 1, j = n - 1, k = m + n - 1;\n        \/\/ \u4ece\u540e\u5f80\u524d\u6bd4\u8f83\u5927\u5c0f\uff0c\u5927\u7684\u8986\u76d60\n        while (i >= 0 &amp;&amp; j >= 0) {\n            nums1&#91;k--] = nums1&#91;i] > nums2&#91;j] ? nums1&#91;i--] : nums2&#91;j--];\n        }\n        \/\/ \u82e5j\u6709\u5269\u4f59\uff0c\u5219\u5168\u90e8\u653e\u5728nums1\u524d\u90e8\u5206\n        while (j >= 0) {\n            nums1&#91;k--] = nums2&#91;j--];\n        }\n    }<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>88. \u5408\u5e76\u4e24\u4e2a\u6709\u5e8f\u6570\u7ec4 \u7ed9\u4f60\u4e24\u4e2a\u6309&nbsp;\u975e\u9012\u51cf\u987a\u5e8f&nbsp;\u6392\u5217\u7684\u6574\u6570\u6570\u7ec4&nbsp;nums1&#038;n [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-296","post","type-post","status-publish","format-standard","hentry","category-leetcode"],"_links":{"self":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/comments?post=296"}],"version-history":[{"count":1,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/296\/revisions"}],"predecessor-version":[{"id":297,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/296\/revisions\/297"}],"wp:attachment":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/media?parent=296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/categories?post=296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/tags?post=296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}