{"id":2042,"date":"2022-01-14T18:19:35","date_gmt":"2022-01-14T10:19:35","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2042"},"modified":"2022-01-14T21:19:02","modified_gmt":"2022-01-14T13:19:02","slug":"leetcodeday23","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/14\/leetcodeday23\/","title":{"rendered":"leetcodeday23 &#8212;\u5408\u5e76K\u4e2a\u5347\u5e8f\u94fe\u8868"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\u6570\u7ec4\uff0c\u6bcf\u4e2a\u94fe\u8868\u90fd\u5df2\u7ecf\u6309\u5347\u5e8f\u6392\u5217\u3002<\/p>\n\n\n\n<p>\u8bf7\u4f60\u5c06\u6240\u6709\u94fe\u8868\u5408\u5e76\u5230\u4e00\u4e2a\u5347\u5e8f\u94fe\u8868\u4e2d\uff0c\u8fd4\u56de\u5408\u5e76\u540e\u7684\u94fe\u8868\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>lists = &#91;&#91;1,4,5],&#91;1,3,4],&#91;2,6]]\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;1,1,2,3,4,4,5,6]\n<strong>\u89e3\u91ca\uff1a<\/strong>\u94fe\u8868\u6570\u7ec4\u5982\u4e0b\uff1a\n&#91;\n  1-&gt;4-&gt;5,\n  1-&gt;3-&gt;4,\n  2-&gt;6\n]\n\u5c06\u5b83\u4eec\u5408\u5e76\u5230\u4e00\u4e2a\u6709\u5e8f\u94fe\u8868\u4e2d\u5f97\u5230\u3002\n1-&gt;1-&gt;2-&gt;3-&gt;4-&gt;4-&gt;5-&gt;6<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>lists = &#91;]\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;]<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>lists = &#91;&#91;]]\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;]<\/code><\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul><li><code>k == lists.length<\/code><\/li><li><code>0 &lt;= k &lt;= 10^4<\/code><\/li><li><code>0 &lt;= lists[i].length &lt;= 500<\/code><\/li><li><code>-10^4 &lt;= lists[i][j] &lt;= 10^4<\/code><\/li><li><code>lists[i]<\/code>&nbsp;\u6309&nbsp;<strong>\u5347\u5e8f<\/strong>&nbsp;\u6392\u5217<\/li><li><code>lists[i].length<\/code>&nbsp;\u7684\u603b\u548c\u4e0d\u8d85\u8fc7&nbsp;<code>10^4<\/code><\/li><\/ul>\n\n\n\n<p>\u601d\u8def\uff1a\u53c2\u8003\u5408\u5e76\u4e24\u4e2a\u6709\u5e8f\u94fe\u8868\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc code=start\n# Definition for singly-linked list.\n# class ListNode:\n#     def __init__(self, val=0, next=None):\n#         self.val = val\n#         self.next = next\nclass Solution:\n    def mergeKLists(self, lists: List&#91;ListNode]) -&gt; ListNode:\n        prehead = ListNode(-1)\n        n=len(lists)\n        x=&#91;&#91;10**4+1]*n]\n        prev = prehead\n        while lists!= &#91;&#91;None]*n]:\n            list1=&#91;]\n            for i in  range(n):\n                if lists&#91;i]:\n                    list1.append(lists&#91;i].val)\n                else:\n                    list1.append(10**4+1)\n            \n            if list1==x&#91;0]:\n                break\n            index=list1.index(min(list1))  \n            prev.next = lists&#91;index]\n            \n            lists&#91;index]= lists&#91;index].next  \n           \n            prev = prev.next\n        return prehead.next<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-220.png\" alt=\"\" class=\"wp-image-2044\" width=\"372\" height=\"158\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-220.png 873w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-220-300x127.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-220-768x326.png 768w\" sizes=\"(max-width: 372px) 100vw, 372px\" \/><\/figure><\/div>\n\n\n\n<p>\u5bf9\u4e8e\u8be5\u4f8b\u5b50\u8d85\u65f6\u4e86<\/p>\n\n\n\n<p>\u770b\u4e0bleetcode\u7ed9\u51fa\u7684\u4e00\u4e9b\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<p>\u4e24\u4e24\u5408\u5e76\uff1a<\/p>\n\n\n\n<p>\u6700\u7b80\u5355\u65b9\u6cd5\uff1a\u91c7\u7528\u5206\u6cbb\u6cd5\uff0c\u5229\u7528\u4e4b\u524d\u7684\u4e24\u4e24\u5408\u5e76\u7684\u94fe\u8868\uff0c\u9010\u4e2a\u5c06K\u4e2a\u94fe\u8868\u5408\u5e76\u3002\u4f46\u8fd9\u6837\u6548\u7387\u8f83\u4f4e\uff0c\u65f6\u95f4\u4e0a\u53ea\u6bd416%\u7684\u63d0\u4ea4\u4f18\u79c0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def mergeKLists(self, lists: List&#91;ListNode]) -&gt; ListNode:\n        if not lists: return None\n        res = None #\u8bbe\u7f6e\u521d\u59cb\u7ed3\u679c\u4e3a\u7a7a\n        for listi in lists: #\u9010\u4e2a\u904d\u5386 \u4e24\u4e24\u5408\u5e76\n            res = self.mergeTwoLists(res, listi)\n        return res\n    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -&gt; ListNode:\n        dummy = ListNode(0) #\u6784\u9020\u865a\u8282\u70b9\n        move = dummy #\u8bbe\u7f6e\u79fb\u52a8\u8282\u70b9\u7b49\u4e8e\u865a\u8282\u70b9\n        while l1 and l2: #\u90fd\u4e0d\u7a7a\u65f6\n            if l1.val &lt; l2.val:\n                move.next = l1 #\u79fb\u52a8\u8282\u70b9\u6307\u5411\u6570\u5c0f\u7684\u94fe\u8868\n                l1 = l1.next\n            else:\n                move.next = l2\n                l2 = l2.next\n            move = move.next\n        move.next = l1 if l1 else l2 #\u8fde\u63a5\u540e\u7eed\u975e\u7a7a\u94fe\u8868\n        return dummy.next #\u865a\u8282\u70b9\u4ecd\u5728\u5f00\u5934<\/code><\/pre>\n\n\n\n<ul><li>\u5f52\u5e76 \uff1a<\/li><\/ul>\n\n\n\n<p>\u6700\u540e\u7684\u4e24\u94fe\u8868\u5408\u5e76\u90e8\u5206\u4e0d\u53d8\uff0c\u4e0a\u90e8\u7684\u4e24\u4e24\u5408\u5e76\u6539\u6210\u5f52\u5e76\u5f62\u5f0f\u7684\u9012\u5f52\u64cd\u4f5c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def mergeKLists(self, lists: List&#91;ListNode]) -&gt; ListNode:\n        if not lists: return None\n        n = len(lists) #\u8bb0\u5f55\u5b50\u94fe\u8868\u6570\u91cf\n        return self.mergeSort(lists, 0, n - 1) #\u8c03\u7528\u5f52\u5e76\u6392\u5e8f\u51fd\u6570\n    def mergeSort(self, lists: List&#91;ListNode], l: int, r: int) -&gt; ListNode:\n        if l == r:\n            return lists&#91;l]\n        m = (l + r) \/\/ 2\n        L = self.mergeSort(lists, l, m) #\u5faa\u73af\u7684\u9012\u5f52\u90e8\u5206\n        R = self.mergeSort(lists, m + 1, r)\n        return self.mergeTwoLists(L, R) #\u8c03\u7528\u4e24\u94fe\u8868\u5408\u5e76\u51fd\u6570\n    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -&gt; ListNode:\n        dummy = ListNode(0) #\u6784\u9020\u865a\u8282\u70b9\n        move = dummy #\u8bbe\u7f6e\u79fb\u52a8\u8282\u70b9\u7b49\u4e8e\u865a\u8282\u70b9\n        while l1 and l2: #\u90fd\u4e0d\u7a7a\u65f6\n            if l1.val &lt; l2.val:\n                move.next = l1 #\u79fb\u52a8\u8282\u70b9\u6307\u5411\u6570\u5c0f\u7684\u94fe\u8868\n                l1 = l1.next\n            else:\n                move.next = l2\n                l2 = l2.next\n            move = move.next\n        move.next = l1 if l1 else l2 #\u8fde\u63a5\u540e\u7eed\u975e\u7a7a\u94fe\u8868\n        return dummy.next #\u865a\u8282\u70b9\u4ecd\u5728\u5f00\u5934<\/code><\/pre>\n\n\n\n<p>\u6700\u5c0f\u5806\uff1a<\/p>\n\n\n\n<p>\u601d\u8def\uff1a\u5bf9\u6240\u6709\u7684list\u4e2d\u5143\u7d20\u5165\u5806\uff0c\u7136\u540e\u4f9d\u6b21\u51fa\u5806\u5230\u65b0\u7684listnode\u4e2d\u3002<\/p>\n\n\n\n<p>\u5229\u7528\u5806\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u6781\u5927\u5730\u7b80\u5316\u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def mergeKLists(self, lists: List&#91;ListNode]) -&gt; ListNode:\n        import heapq #\u8c03\u7528\u5806\n        minHeap = &#91;]\n        for listi in lists: \n            while listi:\n                heapq.heappush(minHeap, listi.val) #\u628alisti\u4e2d\u7684\u6570\u636e\u9010\u4e2a\u52a0\u5230\u5806\u4e2d\n                listi = listi.next\n        dummy = ListNode(0) #\u6784\u9020\u865a\u8282\u70b9\n        p = dummy\n        while minHeap:\n            p.next = ListNode(heapq.heappop(minHeap)) #\u4f9d\u6b21\u5f39\u51fa\u6700\u5c0f\u5806\u7684\u6570\u636e\n            p = p.next\n        return dummy.next <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\u6570\u7ec4\uff0c\u6bcf\u4e2a\u94fe\u8868\u90fd\u5df2\u7ecf\u6309\u5347\u5e8f\u6392\u5217\u3002 \u8bf7\u4f60\u5c06\u6240\u6709\u94fe\u8868\u5408\u5e76\u5230\u4e00\u4e2a\u5347\u5e8f\u94fe\u8868\u4e2d\uff0c\u8fd4\u56de\u5408\u5e76\u540e\u7684\u94fe\u8868\u3002 \u793a\u4f8b 1 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/14\/leetcodeday23\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday23 &#8212;\u5408\u5e76K\u4e2a\u5347\u5e8f\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\/2042"}],"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=2042"}],"version-history":[{"count":6,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2042\/revisions"}],"predecessor-version":[{"id":2062,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2042\/revisions\/2062"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2042"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}