{"id":683,"date":"2020-09-26T11:10:32","date_gmt":"2020-09-26T03:10:32","guid":{"rendered":"http:\/\/39.96.58.60\/?p=683"},"modified":"2022-10-18T16:38:16","modified_gmt":"2022-10-18T08:38:16","slug":"leetcode-725-%e5%88%86%e9%9a%94%e9%93%be%e8%a1%a8","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=683","title":{"rendered":"Leetcode 725. \u5206\u9694\u94fe\u8868"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u5934\u7ed3\u70b9\u4e3a <code>root<\/code> \u7684\u94fe\u8868, \u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u4ee5\u5c06\u94fe\u8868\u5206\u9694\u4e3a <code>k<\/code> \u4e2a\u8fde\u7eed\u7684\u90e8\u5206\u3002<\/p>\n<p>\u6bcf\u90e8\u5206\u7684\u957f\u5ea6\u5e94\u8be5\u5c3d\u53ef\u80fd\u7684\u76f8\u7b49: \u4efb\u610f\u4e24\u90e8\u5206\u7684\u957f\u5ea6\u5dee\u8ddd\u4e0d\u80fd\u8d85\u8fc7 1\uff0c\u4e5f\u5c31\u662f\u8bf4\u53ef\u80fd\u6709\u4e9b\u90e8\u5206\u4e3a null\u3002<\/p>\n<p>\u8fd9k\u4e2a\u90e8\u5206\u5e94\u8be5\u6309\u7167\u5728\u94fe\u8868\u4e2d\u51fa\u73b0\u7684\u987a\u5e8f\u8fdb\u884c\u8f93\u51fa\uff0c\u5e76\u4e14\u6392\u5728\u524d\u9762\u7684\u90e8\u5206\u7684\u957f\u5ea6\u5e94\u8be5\u5927\u4e8e\u6216\u7b49\u4e8e\u540e\u9762\u7684\u957f\u5ea6\u3002<\/p>\n<p>\u8fd4\u56de\u4e00\u4e2a\u7b26\u5408\u4e0a\u8ff0\u89c4\u5219\u7684\u94fe\u8868\u7684\u5217\u8868\u3002<\/p>\n<p>\u4e3e\u4f8b\uff1a 1-&gt;2-&gt;3-&gt;4, k = 5 \/\/ 5 \u7ed3\u679c [ [1], [2], [3], [4], null ]<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> \n\nroot = [1, 2, 3], k = 5\n\n<strong>\u8f93\u51fa:<\/strong> [[1],[2],[3],[],[]]\n\n<strong>\u89e3\u91ca:<\/strong>\n\n\u8f93\u5165\u8f93\u51fa\u5404\u90e8\u5206\u90fd\u5e94\u8be5\u662f\u94fe\u8868\uff0c\u800c\u4e0d\u662f\u6570\u7ec4\u3002\n\n\u4f8b\u5982, \u8f93\u5165\u7684\u7ed3\u70b9 root \u7684 val= 1, root.next.val = 2, \\root.next.next.val = 3, \u4e14 root.next.next.next = null\u3002\n\n\u7b2c\u4e00\u4e2a\u8f93\u51fa output[0] \u662f output[0].val = 1, output[0].next = null\u3002\n\n\u6700\u540e\u4e00\u4e2a\u5143\u7d20 output[4] \u4e3a null, \u5b83\u4ee3\u8868\u4e86\u6700\u540e\u4e00\u4e2a\u90e8\u5206\u4e3a\u7a7a\u94fe\u8868\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> \n\nroot = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3\n\n<strong>\u8f93\u51fa:<\/strong> [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]\n\n<strong>\u89e3\u91ca:<\/strong>\n\n\u8f93\u5165\u88ab\u5206\u6210\u4e86\u51e0\u4e2a\u8fde\u7eed\u7684\u90e8\u5206\uff0c\u5e76\u4e14\u6bcf\u90e8\u5206\u7684\u957f\u5ea6\u76f8\u5dee\u4e0d\u8d85\u8fc71.\u524d\u9762\u90e8\u5206\u7684\u957f\u5ea6\u5927\u4e8e\u7b49\u4e8e\u540e\u9762\u90e8\u5206\u7684\u957f\u5ea6\u3002\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a:<\/strong><\/p>\n<ul>\n<li><code>root<\/code> \u7684\u957f\u5ea6\u8303\u56f4\uff1a&nbsp;<code>[0, 1000]<\/code>.<\/li>\n<li>\u8f93\u5165\u7684\u6bcf\u4e2a\u8282\u70b9\u7684\u5927\u5c0f\u8303\u56f4\uff1a<code>[0, 999]<\/code>.<\/li>\n<li><code>k<\/code>&nbsp;\u7684\u53d6\u503c\u8303\u56f4\uff1a&nbsp;<code>[1, 50]<\/code>.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u94fe\u8868\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a32 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8699.63% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.8 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8612.97% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u5148\u5c06\u94fe\u8868\u62c6\u5206\u4e3a\u5355\u4e2a\u8282\u70b9\uff0c\u5e76\u7edf\u8ba1\u94fe\u957f\u5ea6\n    \u6839\u636e\u94fe\u8868\u957f\u5ea6\u4ee5\u53cak\uff0c\u8ba1\u7b97\uff0c\u6bcf\u6bb5\u7684\u8282\u70b9\u4e2a\u6570\u4ee5\u53ca\u591a\u4f59\u7684\u8282\u70b9\u6570\n    \u6309\u7167\u8ba1\u7b97\u7ed3\u679c\u8fde\u63a5\u94fe\u8868\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def splitListToParts(self, root: ListNode, k: int) -&gt; List[ListNode]:\n        record = [] # \u5217\u8868\u4fdd\u5b58\u62c6\u5206\u6210\u5355\u4e2a\u7684\u8282\u70b9\n        r = root\n        num = 0     # \u7edf\u8ba1\u94fe\u8868\u957f\u5ea6\n        while r:    # \u62c6\u5206\u94fe\u8868\u8282\u70b9\n            r_next = r.next\n            r.next = None\n            record.append(r)\n            r = r_next\n            num += 1\n\n        result = [] # \u6700\u7ec8\u7ed3\u679c\n        remainder = num % k # \u8ba1\u7b97\u5206\u6bb5\u540e\u7684\u591a\u4f59\u8282\u70b9\u6570\n        n = num \/\/ k    # \u6bcf\u6bb5\u7684\u4e2a\u6570\n        if n &lt; 1:\n            return record + [None] * (k - num)\n\n        start_index = 0 # \u8d77\u59cb\u4f4d\u7f6e\n        for i in range(k):\n            if remainder &gt; 0:   # \u5982\u679c\u8fd8\u6709\u591a\u4f59\u7684\u8282\u70b9\uff0c\u5219\u524d\u51e0\u6bb5\u6bcf\u6bb5\u591a\u4e00\u4e2a\u8282\u70b9\n                nodes = record[start_index:start_index + n + 1]\n                start_index += n + 1\n                remainder -= 1  # \u591a\u4f59\u8282\u70b9\u6570-1\n            else:\n                nodes = record[start_index:start_index + n]\n                start_index += n\n\n            nodes += [None]\n            for j, node in enumerate(nodes):    # \u91cd\u7ec4\u6bcf\u6bb5\u94fe\u8868\n                if node:\n                    node.next = nodes[j + 1]\n            result.append(nodes[0]) # \u5c06\u91cd\u7ec4\u540e\u7684\u6bcf\u6bb5\u94fe\u8868\u6dfb\u52a0\u5230\u6700\u7ec8\u7ed3\u679c\u4e2d\n        return result<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u5934\u7ed3\u70b9\u4e3a root \u7684\u94fe\u8868, \u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u4ee5\u5c06\u94fe\u8868\u5206\u9694\u4e3a k \u4e2a\u8fde\u7eed\u7684\u90e8\u5206\u3002 \u6bcf\u90e8\u5206\u7684\u957f\u5ea6\u5e94\u8be5\u5c3d\u53ef\u80fd\u7684&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/683"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=683"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/683\/revisions"}],"predecessor-version":[{"id":684,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/683\/revisions\/684"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=683"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}