{"id":2815,"date":"2020-01-26T23:35:03","date_gmt":"2020-01-26T15:35:03","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2815"},"modified":"2020-01-26T23:35:03","modified_gmt":"2020-01-26T15:35:03","slug":"%e3%80%90leetcode%e3%80%910025-k%e4%b8%aa%e4%b8%80%e7%bb%84%e7%bf%bb%e8%bd%ac%e9%93%be%e8%a1%a8","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/2815\/","title":{"rendered":"\u3010LeetCode\u30110025 K\u4e2a\u4e00\u7ec4\u7ffb\u8f6c\u94fe\u8868"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/reverse-nodes-in-k-group\/\">https:\/\/leetcode-cn.com\/problems\/reverse-nodes-in-k-group\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em># Definition for singly-linked list.\nclass ListNode(object):\n    def __init__(self, x):\n        self.val = x\n        self.next = None\nclass Solution:\n    def reverseKGroup(self, head: ListNode, k: int):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> head: ListNode\n        <\/em><strong><em>:type<\/em><\/strong><em> k: int\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: ListNode\n        \"\"\"\n        <\/em>cur = head\n        count = 0\n        while cur and count != k:\n            cur = cur.next\n            count += 1\n        if count == k:\n            cur = self.reverseKGroup(cur, k)\n            while count:\n                tmp = head.next\n                head.next = cur\n                cur = head\n                head = tmp\n                count -= 1\n            head = cur\n        return head\nif __name__ == '__main__':\n    n1 = ListNode(1)\n    n2 = ListNode(2)\n    n3 = ListNode(3)\n    n4 = ListNode(4)\n    n5 = ListNode(5)\n    n1.next = n2\n    n2.next = n3\n    n3.next = n4\n    n4.next = n5\n    print(Solution().reverseKGroup(n1, 5))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a\u8fd9\u9053\u9898\u521a\u5f00\u59cb\u6211\u7684\u601d\u8def\u662f\u4ea4\u6362\uff0c\u4e5f\u5c31\u662f\u8bf4\u4ea4\u63620\u548ck-1\u4e2a\u94fe\u8868\uff0c\u7136\u540e\u662f1\u548ck-2\u4e00\u76f4\u5230\u4e2d\u95f4\u4f4d\u7f6e\uff0c\u7136\u540e\u5411\u4e0b\u9012\u5f52\uff0c\u540e\u6765\u53d1\u73b0\u8fd9\u4e2a\u65b9\u6cd5\u5b9e\u73b0\u8d77\u6765\u6709\u4e00\u5b9a\u96be\u5ea6\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u94fe\u8868\u662f\u5355\u5411\u94fe\u8868\uff0c\u65e0\u6cd5\u5411\u524d\u627ek-2\u8282\u70b9\uff0c\u800c\u6bcf\u6b21\u90fd\u904d\u5386\u67e5\u627e\u7684\u4ee3\u4ef7\u6709\u6709\u70b9\u9ad8\uff0c\u56e0\u6b64\u91c7\u7528\u79fb\u52a8\u800c\u4e0d\u662f\u4ea4\u6362\u7684\u65b9\u6cd5\u6765\u8fdb\u884c\u4ea4\u6362\u3002\u7b80\u5355\u8bf4\u5c31\u662f\u6bcf\u6b21\u90fd\u5c06\u4e0d\u5728\u4ea4\u6362\u5e8f\u5217\u4e2d\u7684\u94fe\u8868\uff08\u4e5f\u5c31\u662f\u4e0d\u662f\u524dk\u4e2a\u4ee5\u53ca\u5df2\u7ecf\u4ea4\u6362\u8fc7\u7684\u8282\u70b9\uff09\u6254\u5230\u5934\u7ed3\u70b9\u7684\u540e\u9762\uff0c\u8fd9\u6837\u5934\u8282\u70b9\u5c31\u540e\u79fb\u4e86\uff0c\u4e5f\u5c31\u8fdb\u884c\u4ea4\u6362\u4e86\u3002\uff08\u7b80\u5355\u56fe\u793a\u5982\u4e0b\uff0c\u778e\u753b\u51d1\u5408\u770bemm\uff09<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"623\" height=\"523\" src=\"\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-48.png\" alt=\"\" class=\"wp-image-2816\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-48.png 623w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-48-300x252.png 300w\" sizes=\"(max-width: 623px) 100vw, 623px\" \/><\/figure><\/div>\n\n\n<p><strong>\u601d\u8003<\/strong>\uff1a\u8fd8\u6709\u4e00\u4e2a\u6bd4\u8f83\u597d\u7684\u601d\u8def\u5c31\u662f\u7528\u6808\uff0c\u514d\u53bb\u4e86\u94fe\u8868\u8fde\u6765\u8fde\u53bb\u7684\u95ee\u9898\u3002<\/p>\n\n\n<p>\u6808\u7684\u7279\u6027\uff0c\u5148\u8fdb\u540e\u51fa\u521a\u597d\u6ee1\u8db3\u4ea4\u6362\u539f\u5219\uff0c1234\u8fdb\u6808\u51fa\u6808\u5219\u4e3a4321\uff0c\u56e0\u6b64\u5c06\u94fe\u8868\u4f9d\u6b21\u5165\u6808\uff0c\u51fa\u6808\u4e4b\u540e\u4f9d\u6b21\u94fe\u8d77\u6765\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/reverse-nodes-in- [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10],"tags":[],"views":1394,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2815"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2815"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2815\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2815"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}