{"id":2809,"date":"2020-01-23T15:41:21","date_gmt":"2020-01-23T07:41:21","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2809"},"modified":"2020-01-23T15:41:21","modified_gmt":"2020-01-23T07:41:21","slug":"%e3%80%90leetcode%e3%80%910023-%e5%90%88%e5%b9%b6k%e4%b8%aa%e6%8e%92%e5%ba%8f%e9%93%be%e8%a1%a8","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/2809\/","title":{"rendered":"\u3010LeetCode\u30110023 \u5408\u5e76k\u4e2a\u6392\u5e8f\u94fe\u8868"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/merge-k-sorted-lists\/\">https:\/\/leetcode-cn.com\/problems\/merge-k-sorted-lists\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-<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(object):\n    def mergeKLists(self, lists):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> lists: List[ListNode]\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: ListNode\n        \"\"\"\n        <\/em>res = ListNode(None)\n        point = res\n        while len(lists) > 0:\n            min_val = None\n            for each in lists:\n                if each is None:\n                    # lists.remove(each)\n                    continue\n                if min_val is None or each.val &lt; min_val.val:\n                    min_val = each\n            if min_val is None:\n                return res.next\n            point.next = min_val\n            point = point.next\n            for i in range(len(lists)):\n                if lists[i] is not None and lists[i].val == min_val.val:\n                    print(min_val.val)\n                    lists[i] = lists[i].next\n                    break\n        return res.next\nif __name__ == '__main__':\n    n1 = ListNode(1)\n    n2 = ListNode(4)\n    n3 = ListNode(5)\n    n4 = ListNode(1)\n    n5 = ListNode(3)\n    n6 = ListNode(4)\n    n7 = ListNode(2)\n    n8 = ListNode(6)\n    n1.next = n2\n    n2.next = n3\n    n4.next = n5\n    n5.next = n6\n    n7.next = n8\n    lists = [n1, n4, n7]\n    print(Solution().mergeKLists1(lists))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a\u8fd9\u9053\u9898\u7684\u601d\u8def\u5c31\u662f\u9996\u5148\u5bf9\u6bd4k\u4e2a\u94fe\u8868\u7684\u9996\u4f4d\uff0c\u627e\u51fa\u6700\u5c0f\u7684\u90a3\u4e00\u4e2a\u7136\u540e\u52a0\u5230\u7ed3\u679c\u94fe\u8868\u4e2d\u7ee7\u7eed\u5411\u4e0b\u67e5\u627e\u3002<\/p>\n\n\n<p><strong>\u601d\u8003<\/strong>\uff1a\u89c2\u5bdf\u8bc4\u8bba\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0\u8fd8\u6709\u4e00\u4e2a\u6bd4\u8f83\u7b80\u5355\u7684\u505a\u6cd5\uff0c\u5c31\u662f\u4f18\u5148\u961f\u5217\uff0c\u7c7b\u4f3c\u4e8e\u751f\u6210\u6811\u548c\u6700\u5927\uff08\u5c0f\uff09\u5806\uff0c\u6bcf\u6b21\u63d2\u5165\u7ef4\u62a4\u6570\u636e\u7ed3\u6784\u7279\u6027\uff0c\u51fa\u7684\u65f6\u5019\u8f93\u51fa\u6700\u5c0f\u7684\uff0c\u8fd9\u79cd\u7ed3\u6784\u5f80\u5f80\u662flgn\u7ea7\u522b\u7684\u590d\u6742\u5ea6\uff0c\u56e0\u6b64\u76f8\u6bd4\u4e8e\u4e00\u822c\u7b97\u6cd5\u786e\u5b9e\u5feb\u4e86\u4e0d\u5c11\u3002\u4e0b\u9762\u8d34\u4e00\u4e2a\u4f18\u5148\u961f\u5217\u5b9e\u73b0\u7684\u7248\u672c\u3002<\/p>\n\n\n<pre class=\"wp-block-preformatted\">def mergeKLists1(self, lists):<br \/>    <em>\"\"\"<br \/><\/em><em>    <\/em><strong><em>:type<\/em><\/strong><em> lists: List[ListNode]<br \/><\/em><em>    <\/em><strong><em>:rtype<\/em><\/strong><em>: ListNode<br \/><\/em><em>    \"\"\"<br \/><\/em><em>    <\/em>from queue import PriorityQueue<br \/>    head = point = ListNode(0)<br \/>    q = PriorityQueue()<br \/>    for each in lists:<br \/>        while each is not None:<br \/>            q.put(each.val)<br \/>            each = each.next<br \/>    while not q.empty():<br \/>        val = q.get()<br \/>        point.next = ListNode(val)<br \/>        point = point.next<br \/>    return head.next<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/merge-k-sorted-li [&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":1415,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2809"}],"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=2809"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2809\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2809"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}