{"id":2352,"date":"2022-01-24T16:10:42","date_gmt":"2022-01-24T08:10:42","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2352"},"modified":"2022-01-24T16:10:43","modified_gmt":"2022-01-24T08:10:43","slug":"leetcodeday25","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/24\/leetcodeday25\/","title":{"rendered":"leetcodeday25 &#8212; K \u4e2a\u4e00\u7ec4\u7ffb\u8f6c\u94fe\u8868"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\uff0c\u6bcf&nbsp;<em>k&nbsp;<\/em>\u4e2a\u8282\u70b9\u4e00\u7ec4\u8fdb\u884c\u7ffb\u8f6c\uff0c\u8bf7\u4f60\u8fd4\u56de\u7ffb\u8f6c\u540e\u7684\u94fe\u8868\u3002<\/p>\n\n\n\n<p><em>k&nbsp;<\/em>\u662f\u4e00\u4e2a\u6b63\u6574\u6570\uff0c\u5b83\u7684\u503c\u5c0f\u4e8e\u6216\u7b49\u4e8e\u94fe\u8868\u7684\u957f\u5ea6\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u8282\u70b9\u603b\u6570\u4e0d\u662f&nbsp;<em>k&nbsp;<\/em>\u7684\u6574\u6570\u500d\uff0c\u90a3\u4e48\u8bf7\u5c06\u6700\u540e\u5269\u4f59\u7684\u8282\u70b9\u4fdd\u6301\u539f\u6709\u987a\u5e8f\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/10\/03\/reverse_ex1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>head = &#91;1,2,3,4,5], k = 2\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;2,1,4,3,5]<\/code><\/pre>\n\n\n\n<p>\u6cd5 \u4e00\uff1a\u53ea\u4ea4\u6362\u503c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># &#91;25] K \u4e2a\u4e00\u7ec4\u7ffb\u8f6c\u94fe\u8868\r\n#\r\n#\u6cd51 \uff1a\u53ea\u4ea4\u6362\u503c\r\n# @lc code=start\r\n# Definition for singly-linked list.\r\n# class ListNode:\r\n#     def __init__(self, val=0, next=None):\r\n#         self.val = val\r\n#         self.next = next\r\n\r\n\r\nclass Solution:\r\n    def reverseKGroup(self, head: Optional&#91;ListNode], k: int) -> Optional&#91;ListNode]:\r\n        head1=head\r\n        fhead= ListNode(0,head)\r\n        head2=fhead\r\n        end=0\r\n        while head1!= None:\r\n            nums=list()\r\n            for i in range(k):\r\n                head2=head2.next\r\n                if head2==None:\r\n                    end=1\r\n                    break \r\n                #print(head2.val)   \r\n                nums.append(head2.val)\r\n            if end==0:\r\n                for i in range(k):\r\n                    head1.val=nums&#91;k-1-i]\r\n                    head1=head1.next\r\n            else:\r\n                for i in range(len(nums)):\r\n                    head1.val=nums&#91;i]\r\n                    head1=head1.next\r\n        return  fhead.next   \r\n# @lc code=end\r\n\r\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"800\" height=\"261\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-284.png\" alt=\"\" class=\"wp-image-2353\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-284.png 800w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-284-300x98.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-284-768x251.png 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<p class=\"has-light-pink-background-color has-background\"><strong>\u8fdb\u9636\uff1a<\/strong><\/p>\n\n\n\n<ul><li>\u4f60\u53ef\u4ee5\u8bbe\u8ba1\u4e00\u4e2a\u53ea\u4f7f\u7528\u5e38\u6570\u989d\u5916\u7a7a\u95f4\u7684\u7b97\u6cd5\u6765\u89e3\u51b3\u6b64\u95ee\u9898\u5417\uff1f<\/li><li><strong>\u4f60\u4e0d\u80fd\u53ea\u662f\u5355\u7eaf\u7684\u6539\u53d8\u8282\u70b9\u5185\u90e8\u7684\u503c<\/strong>\uff0c\u800c\u662f\u9700\u8981\u5b9e\u9645\u8fdb\u884c\u8282\u70b9\u4ea4\u6362\u3002<\/li><\/ul>\n\n\n\n<p>\u6cd5\u4e8c\uff1a\u8282\u70b9\u4ea4\u6362\uff08\u4e0d\u53ea\u662f\u5355\u7eaf\u6539\u53d8\u503c\uff09 \u4ee3\u505a\u3002\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\uff0c\u6bcf&nbsp;k&nbsp;\u4e2a\u8282\u70b9\u4e00\u7ec4\u8fdb\u884c\u7ffb\u8f6c\uff0c\u8bf7\u4f60\u8fd4\u56de\u7ffb\u8f6c\u540e\u7684\u94fe\u8868\u3002 k&nbsp;\u662f\u4e00\u4e2a\u6b63\u6574 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/24\/leetcodeday25\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday25 &#8212; K \u4e2a\u4e00\u7ec4\u7ffb\u8f6c\u94fe\u8868<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2352"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2352"}],"version-history":[{"count":3,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2352\/revisions"}],"predecessor-version":[{"id":2356,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2352\/revisions\/2356"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2352"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}