加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

[LeetCode] 023. Merge k Sorted Lists (Hard) (C++/Java/Python

发布时间:2020-12-13 20:04:54 所属栏目:PHP教程 来源:网络整理
导读:索引:[LeetCode] Leetcode 题解索引 (C/Java/Python/Sql) Github: https://github.com/illuz/leetcode 023. Merge k Sorted Lists (Hard) 链接 : 题目:https://oj.leetcode.com/problems/merge-k-sorted-lists/ 代码(github):https://github.com/illuz/l

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)
Github: https://github.com/illuz/leetcode


023. Merge k Sorted Lists (Hard)

链接

题目:https://oj.leetcode.com/problems/merge-k-sorted-lists/
代码(github):https://github.com/illuz/leetcode

题意

和 021. Merge Two Sorted Lists (Easy) 类似,这次要 Merge K 个。

分析

很明显可以想到利用已完成的 Merge Two Sorted Lists 的函数来用。
这时候有两种方法:
1. (C++) 用2分的思想,把每一个 List 和它相邻的 List 进行 Merge,这样范围就缩小了1半了,再重复这样,就能够 O(nklogk) 完成。比如: [1,2,…,n] 的第1轮 Merge 是 [1,n/2],[2,n/2+1],…
2. (Python) 也是用2分的思想,就是把 Lists 分为两部份,分别递归 Merge k Sorted Lists 后变成两个 List ,然后再对这两 List 进行 Merge Two Sorted Lists 。

这两种方法都是递归调用,都可以进行记忆化,用空间换时间,不过我不清楚会不会超空间(Memory Limit Exceed),所以就没试了~

除用2分的思路,还有更好写的方法,就是用堆(heap),具体就是用优先队列(Priority Queue)。
(Java) 先把每一个 List 的第1个节点放进优先队列,每次取出队列中的最大值节点,再把那个节点的 next 放进去。

代码

C++:

class Solution { public: ListNode *mergeKLists(vector<ListNode *> &lists) { int sz = lists.size(); if (sz == 0) return NULL; while (sz > 1) { int k = (sz + 1) / 2; for (int i = 0; i < sz / 2; i++) lists[i] = mergeTwoLists(lists[i],lists[i + k]); sz = k; } return lists[0]; } ListNode *mergeTwoLists(ListNode *l1,ListNode *l2) { if (l1 == NULL) return l2; if (l2 == NULL) return l1; ListNode *start,*p1; if (l1->val < l2->val) { p1 = start = l1; l1 = l1->next; } else { p1 = start = l2; l2 = l2->next; } while (l1 != NULL && l2 != NULL) { if (l1->val < l2->val) { p1->next = l1; p1 = l1; l1 = l1->next; } else { p1->next = l2; p1 = l2; l2 = l2->next; } } if (l1 != NULL) p1->next = l1; else p1->next = l2; return start; } };


Java:

public class Solution { public ListNode mergeKLists(List<ListNode> lists) { Queue<ListNode> heap = new PriorityQueue<ListNode>(new Comparator<ListNode>(){ @Override public int compare(ListNode l1,ListNode l2) { return l1.val - l2.val; } }); ListNode dummy = new ListNode(0),cur = dummy,tmp; for (ListNode list : lists) { if (list != null) { heap.offer(list); } } while (!heap.isEmpty()) { tmp = heap.poll(); cur.next = tmp; cur = cur.next; if (tmp.next != null) { heap.offer(tmp.next); } } return dummy.next; } }


Python:

class Solution: # @param a list of ListNode # @return a ListNode def mergeKLists(self,lists): if len(lists) == 0: return None if len(lists) == 1: return lists[0] mid = len(lists) // 2 left = self.mergeKLists(lists[:mid]) right = self.mergeKLists(lists[mid:]) # merge left and right dummy = ListNode(0) cur = dummy while left or right: if right == None or (left and left.val <= right.val): cur.next = left left = left.next else: cur.next = right right = right.next cur = cur.next return dummy.next


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读