{"id":1956,"date":"2023-09-27T09:27:49","date_gmt":"2023-09-27T01:27:49","guid":{"rendered":"https:\/\/white-night.club\/?p=1956"},"modified":"2023-11-09T11:07:52","modified_gmt":"2023-11-09T03:07:52","slug":"ad17","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/09\/27\/ad17\/","title":{"rendered":"19.\u00a0\u5220\u9664\u94fe\u8868\u7684\u5012\u6570\u7b2c N \u4e2a\u7ed3\u70b9"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\uff0c\u5220\u9664\u94fe\u8868\u7684\u5012\u6570\u7b2c&nbsp;<code>n<\/code><em>&nbsp;<\/em>\u4e2a\u7ed3\u70b9\uff0c\u5e76\u4e14\u8fd4\u56de\u94fe\u8868\u7684\u5934\u7ed3\u70b9\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><img decoding=\"async\" alt=\"\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/10\/03\/remove_ex1.jpg\"><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>head = [1,2,3,4,5], n = 2\n<strong>\u8f93\u51fa\uff1a<\/strong>[1,2,3,5]\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>head = [1], n = 1\n<strong>\u8f93\u51fa\uff1a<\/strong>[]\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>head = [1,2], n = 1\n<strong>\u8f93\u51fa\uff1a<\/strong>[1]\n<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul>\n<li>\u94fe\u8868\u4e2d\u7ed3\u70b9\u7684\u6570\u76ee\u4e3a&nbsp;<code>sz<\/code><\/li>\n\n\n\n<li><code>1 &lt;= sz &lt;= 30<\/code><\/li>\n\n\n\n<li><code>0 &lt;= Node.val &lt;= 100<\/code><\/li>\n\n\n\n<li><code>1 &lt;= n &lt;= sz<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>\u8fdb\u9636\uff1a<\/strong>\u4f60\u80fd\u5c1d\u8bd5\u4f7f\u7528\u4e00\u8d9f\u626b\u63cf\u5b9e\u73b0\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>\u6307\u9488\uff0c\u5f88\u795e\u5947\u5427\uff1f<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u5148\u4e0d\u7ba1\u5b83\u8bf4\u7684\u201c\u4e00\u8d9f\u626b\u63cf\u201d\u89e3\u51b3\u3002\u5220\u9664\u7684\u662f\u201c\u5012\u6570\u201d\u7b2cn\u4e2a\u8282\u70b9\u3002\u90a3\u4e48\u5148\u83b7\u53d6\u6574\u4e2a\u94fe\u8868\u7684\u957f\u5ea6\uff0c\u8fd9\u662f\u80af\u5b9a\u8981\u6709\u7684\u3002\u518d\u770b\u770b\u5012\u6570\u7b2cn\u4e2a\u662f\u6b63\u6570\u7b2c\u51e0\u4e2a\u3002\u867d\u7136\u8fd9\u79cd\u6211\u7ecf\u5e38\u770b\u5230\uff0c\u4f46\u5e73\u65f6\u5c31\u662f\u8981\u60f3\u4e00\u4f1a\uff0c\u4f5c\u4e2a\u56fe\u65b9\u4fbf\u81ea\u5df1\u7406\u89e3\u5427\u3002<\/p>\n\n\n\n<p>\u5047\u8bbe\u5220\u9664\u5012\u6570\u7b2ci\u4e2a\uff0c\u90a3\u5b83\u5de6\u8fb9\u5c31\u6709length-i\u4e2a\u5143\u7d20\u3002\u90a3\u88ab\u5220\u9664\u7684\u8282\u70b9\u4ece\u5de6\u8fb9\u6570\u8d77\u4e0d\u5c31\u662f\u7b2c(length-i)+1\u4e2a\u5143\u7d20\u5417\uff1f<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u5b83\u7684\u524d\u4e00\u4e2a\u8282\u70b9\u521a\u521a\u597d\u5c31\u662f\u94fe\u8868\u7684\u7b2clength-i\u4e2a\u8282\u70b9<\/mark>\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full has-custom-border\"><img loading=\"lazy\" decoding=\"async\" width=\"832\" height=\"415\" src=\"https:\/\/white-night.club\/wp-content\/uploads\/2023\/09\/\u672a\u547d\u540d\u6587\u4ef6-\u5bfc\u51fa-1.png\" alt=\"\" class=\"wp-image-1962\" style=\"border-style:none;border-width:0px;border-radius:0px\" srcset=\"https:\/\/white-night.club\/wp-content\/uploads\/2023\/09\/\u672a\u547d\u540d\u6587\u4ef6-\u5bfc\u51fa-1.png 832w, https:\/\/white-night.club\/wp-content\/uploads\/2023\/09\/\u672a\u547d\u540d\u6587\u4ef6-\u5bfc\u51fa-1-300x150.png 300w, https:\/\/white-night.club\/wp-content\/uploads\/2023\/09\/\u672a\u547d\u540d\u6587\u4ef6-\u5bfc\u51fa-1-768x383.png 768w\" sizes=\"(max-width: 832px) 100vw, 832px\" \/><\/figure>\n\n\n\n<p>\u63a5\u4e0b\u6765\u5c31\u662f\u5220\u8282\u70b9\u3002leetcode\u4e0a\u9762\u7684\u94fe\u8868\u9898\u666e\u904d\u5f00\u5934\u7684head\u8282\u70b9\u90fd\u4e0d\u4e3a\u7a7a\uff0c\u6240\u4ee5\u8981\u5220\u8282\u70b9\u7684\u8bdd\u53ef\u4ee5\u65b0\u5efa\u4e00\u4e2adummy\u8282\u70b9\u3002\u81f3\u4e8e\u4e3a\u4ec0\u4e48\u65b0\u5efa\u8fd9\u4e48\u4e2a\u73a9\u610f&#8230;.\u63a5\u4e0b\u6765\u89e3\u91ca\u3002<\/p>\n\n\n\n<p>\u5220\u9664\u8282\u70b9\u7684\u65f6\u5019\uff0c\u6709\u4e00\u79cd\u6bd4\u8f83\u7279\u6b8a\u7684\u60c5\u51b5\uff1a\u94fe\u8868\u4e2d\u53ea\u6709\u4e00\u4e2a\u5143\u7d20or\u5220\u9664\u7684\u662f\u5934\u8282\u70b9\u3002\u8fd9\u4e24\u4e2a\u90fd\u9700\u8981\u4f60\u53bb\u64cd\u4f5c\u94fe\u8868\u7684\u5934\u8282\u70b9\u3002\u5f53\u7136\u5355\u72ec\u5199\u4e2aif\u8bed\u53e5\u4f5c\u5224\u65ad\u4e5f\u53ef\u4ee5\u3002\u4f46\u662f\u6211\u4eec\u4e5f\u53ef\u4ee5\u5f80<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u94fe\u8868\u524d\u9762\u52a0\u4e00\u4e2adummy\u8282\u70b9<\/mark>\u3002\u8fd9\u6837\u201c\u5934\u8282\u70b9\u201d\u5c31\u53d8\u6210\u4e86&#8221;\u94fe\u8868\u4e2d\u7684\u67d0\u4e00\u4e2a\u8282\u70b9\u201c\uff0c\u4e0d\u7ba1\u5220\u9664\u5b83\u8fd8\u662f\u5176\u4ed6\u4ec0\u4e48\u7684\u90fd\u662f\u548c\u94fe\u8868\u4e2d\u5176\u4ed6\u8282\u70b9\u4e00\u6837\u7684\u64cd\u4f5c\uff0c\u7701\u7684\u591a\u5199\u548c\u591a\u60f3\u3002<\/p>\n\n\n\n<p>\u4e0d\u8fc7\u6700\u540e<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u8981\u8fd4\u56dedummy-next<\/mark>\u3002\u56e0\u4e3a\u539f\u5148\u7684head\u6307\u9488\u65e0\u8bba\u662f\u5730\u5740\u8fd8\u662f\u503c\uff0c\u90fd\u6ca1\u6709\u53d1\u751f\u53d8\u5316\u3002\u6240\u4ee5\u65e0\u8bba\u4f60\u600e\u4e48\u8fd4\u56dehead\u90fd\u662f\u4e00\u5f00\u59cb<em>ListNode* removeNthFromEnd(ListNode* head, int n)<\/em>\u8fd9\u91cc\u4f20\u8fdb\u6765\u7684head\u5730\u5740\u3002\u8fd9\u4e2a\u4e3b\u8981\u8fd8\u662f\u9488\u5bf9\u4e0a\u9762\u63d0\u5230\u7684\u201c\u5220\u9664\u5934\u8282\u70b9\u201d\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u6709\u70b9\u96be\u7406\u89e3\uff1f\u4f5c\u4e2a\u56fe\u5148\uff0c\u6211\u4e2a\u4eba\u611f\u89c9\u94fe\u8868\u9898\u4f5c\u56fe\u5c31\u5f88\u597d\u7406\u89e3\u4e86\u3002\u5982\u56fe\u6240\u89c1\uff0c\u53ea\u8981\u4f60\u6ca1\u5199\u91ca\u653e\uff08delete head\uff09\u5934\u8282\u70b9\u7684\u4ee3\u7801\uff0c\u90a3\u5b83\u5c31\u4e00\u76f4\u5728\u5185\u5b58\u4e2d\u5446\u7740\u3002\u5982\u679c\u4f60\u6ca1\u5bf9\u5934\u8282\u70b9\u4f5c\u64cd\u4f5c\uff0c\u90a3dummy->next=head\uff1b\u5982\u679c\u5220\u4e86\u5934\u8282\u70b9\uff0c\u90a3dummy->next=head->next\u3002\u4e5f\u5c31\u662f\u8bf4\u4e0d\u7ba1\u600e\u4e48\u6837dummy->next\u59cb\u7ec8\u662f\u94fe\u8868\u7684\u5934\u8282\u70b9\uff0c\u90a3\u76f4\u63a5\u8fd4\u56de\u5b83\u5c31\u597d\u4e86\u3002<\/p>\n\n\n\n<p>\u5f53\u7136\u4f60\u4e5f\u53ef\u4ee5\u7528\u51e0\u4e2aif\u8bed\u53e5\u5206\u522b\u4f5c\u5224\u65ad\uff0c\u4f46\u6211\u89c9\u5f97\u4ee3\u7801\u8fd8\u662f\u7b80\u6d01\u4e00\u70b9\u6bd4\u8f83\u597d\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large has-custom-border\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"526\" src=\"https:\/\/white-night.club\/wp-content\/uploads\/2023\/09\/\u672a\u547d\u540d\u6587\u4ef6-\u5bfc\u51fa-1-1-1024x526.png\" alt=\"\" class=\"wp-image-1976\" style=\"border-style:none;border-width:0px;border-radius:0px\" srcset=\"https:\/\/white-night.club\/wp-content\/uploads\/2023\/09\/\u672a\u547d\u540d\u6587\u4ef6-\u5bfc\u51fa-1-1-1024x526.png 1024w, https:\/\/white-night.club\/wp-content\/uploads\/2023\/09\/\u672a\u547d\u540d\u6587\u4ef6-\u5bfc\u51fa-1-1-300x154.png 300w, https:\/\/white-night.club\/wp-content\/uploads\/2023\/09\/\u672a\u547d\u540d\u6587\u4ef6-\u5bfc\u51fa-1-1-768x395.png 768w, https:\/\/white-night.club\/wp-content\/uploads\/2023\/09\/\u672a\u547d\u540d\u6587\u4ef6-\u5bfc\u51fa-1-1.png 1236w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>ListNode*dummy=new ListNode(0,head);\n<mark style=\"background-color:rgba(0, 0, 0, 0);color:#00ff2f\" class=\"has-inline-color\">end=dummy;<\/mark>\nfor(int i=1;i&lt;=length-n+1;i++)\n{     \n     if(i==length-n+1)\n     {\n         <mark style=\"background-color:rgba(0, 0, 0, 0);color:#00ff2f\" class=\"has-inline-color\">end-&gt;next=end-&gt;next-&gt;next;<\/mark>\n     }\n     end=end-&gt;next;\n}\nreturn dummy-&gt;next;<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center\"><strong>\u5b8c\u6574\u4ee3\u7801\uff08\u4e24\u6b21\u626b\u63cf\uff09<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode() : val(0), next(nullptr) {}\n *     ListNode(int x) : val(x), next(nullptr) {}\n *     ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n *\/\nclass Solution {\npublic:\n    ListNode* removeNthFromEnd(ListNode* head, int n) {\n        ListNode*end=head;\n        int length=1;\n        while(end-&gt;next)\n        {\n            length++;\n            end=end-&gt;next;\n        }\n        ListNode*dummy=new ListNode(0,head);\n        end=dummy;\n        for(int i=1;i&lt;=length-n+1;i++)\n        {\n            \n            if(i==length-n+1)\n            {\n                end-&gt;next=end-&gt;next-&gt;next;\n            }\n            end=end-&gt;next;\n        }\n        return dummy-&gt;next;\n    }\n};<\/code><\/pre>\n\n\n\n<p>\u63a5\u4e0b\u6765\u6765\u770b\u770b\u53cc\u6307\u9488\u662f\u600e\u4e48\u5199\u7684\u3002<\/p>\n\n\n\n<p>\u53cc\u6307\u9488\uff0c\u987e\u540d\u601d\u4e49\u7528\u4e24\u4e2a\u6307\u9488+\u4e00\u6b21\u626b\u63cf\u94fe\u8868\u3002\u4e3a\u4e86\u65b9\u4fbf\u5220\u5934\u8282\u70b9\uff0c\u8fd8\u662f\u548c\u4e0a\u9762\u4e00\u6837\uff0c\u5e26\u4e0a\u4e2adummy\u8282\u70b9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ListNode*dummy=new ListNode(0,head);\nListNode*first=head;\nListNode*second=dummy;<\/code><\/pre>\n\n\n\n<p>\u81f3\u4e8e\u4e3a\u4ec0\u4e48first\u548csecond\u53d6\u8fd9\u4e24\u4e2a\u8282\u70b9\u3002\u4f9d\u636e\u7684\u8fd8\u662f\u8fd9\u5f20\u56fe\u3002dummy\u76f8\u5f53\u4e8e\u4ecelength=0\u7684\u5730\u65b9\u5f00\u59cb\u7b97\u8d77\u3002\u5982\u679cfirst\u6307\u9488\u5148\u51fa\u53d1\u8d70i\u6b65\uff0c\u6b64\u65f6<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u5b83\u8ddd\u79bb\u8be5\u94fe\u8868\u7684\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u8fd8\u6709length-i\u6b65\uff08\u603b\u548c\u4e3alength\uff09<\/mark>\u3002\u5982\u679c\u6b64\u65f6second\u6307\u9488\u548cfirst\u6307\u9488\u518d\u540c\u65f6\u51fa\u53d1\uff0c\u90fd\u8d70length-i\u6b65\u3002\u6b64\u65f6second\u6307\u9488\u6307\u5411\u94fe\u8868\u5c3e\u90e8\u7684null\uff0c\u800cfirst\u6307\u9488\u521a\u521a\u597d\u6307\u5411i\u8282\u70b9\u7684\u524d\u4e00\u4e2a\u8282\u70b9\u3002\u5f88\u795e\u5947\u5427\ud83d\ude49<\/p>\n\n\n\n<figure class=\"wp-block-image size-large has-custom-border\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"549\" src=\"https:\/\/white-night.club\/wp-content\/uploads\/2023\/09\/\u672a\u547d\u540d\u6587\u4ef6-\u5bfc\u51fa-2-1024x549.png\" alt=\"\" class=\"wp-image-1980\" style=\"border-style:none;border-width:0px;border-radius:0px\" srcset=\"https:\/\/white-night.club\/wp-content\/uploads\/2023\/09\/\u672a\u547d\u540d\u6587\u4ef6-\u5bfc\u51fa-2-1024x549.png 1024w, https:\/\/white-night.club\/wp-content\/uploads\/2023\/09\/\u672a\u547d\u540d\u6587\u4ef6-\u5bfc\u51fa-2-300x161.png 300w, https:\/\/white-night.club\/wp-content\/uploads\/2023\/09\/\u672a\u547d\u540d\u6587\u4ef6-\u5bfc\u51fa-2-768x412.png 768w, https:\/\/white-night.club\/wp-content\/uploads\/2023\/09\/\u672a\u547d\u540d\u6587\u4ef6-\u5bfc\u51fa-2.png 1516w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-center\"><strong>\u5b8c\u6574\u4ee3\u7801\uff08\u53cc\u6307\u9488\uff09<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode() : val(0), next(nullptr) {}\n *     ListNode(int x) : val(x), next(nullptr) {}\n *     ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n *\/\nclass Solution {\npublic:\n    ListNode* removeNthFromEnd(ListNode* head, int n) {\n       ListNode*dummy=new ListNode(0,head);\n       ListNode*first=head;\n       ListNode*second=dummy;\n       for(int i=0;i&lt;n;i++)\n       {\n           first=first-&gt;next;\n       }\n       while(first)\n       {\n           first=first-&gt;next;\n           second=second-&gt;next;\n       }\n       second-&gt;next=second-&gt;next-&gt;next;\n\n       return dummy-&gt;next;\n    }\n};<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Leetcode\u9898\u76ee\uff1a\u66b4\u529b&#038;&#038;\u53cc\u6307\u9488<\/p>\n","protected":false},"author":1,"featured_media":1962,"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\/1956"}],"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=1956"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/1956\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/1962"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=1956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=1956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=1956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}