150強訪談簡單

合併 K 個排序列表

「合併 K 排序清單」問題的詳細指南和 Python 實作。

問題陳述

簡單

給定一個由 k 個鍊錶組成的陣列,每個鍊錶都按升序排序。

將所有鍊錶合併為一個已排序的鍊錶並傳回。

連結列表表示為 Python 列表的列表。實作一個函數 mergeKLists(lists: list) -> list 傳回合併的排序清單。

約束條件
  • k == lists.length
  • 0 <= k <= 10000
  • 0 <= lists[i].length <= 500
  • -10000 <= lists[i][j] <= 10000
  • lists[i] is sorted in ascending order
  • The sum of lists[i].length will not exceed 10000

範例

Example 1
Input
[[1,4,5],[1,3,4],[2,6]]
Output
[1,1,2,3,4,4,5,6]
Explanation

Merging [1,4,5], [1,3,4], and [2,6] gives [1,1,2,3,4,4,5,6].

Example 2
Input
[]
Output
[]
Explanation

No lists to merge, so the result is empty.

Example 3
Input
[[]]
Output
[]
Explanation

One empty list results in an empty merged list.

Need a Hint?
考慮使用特定於連結清單的資料結構,例如集合或堆。
Edge Cases to Watch
  • 空輸入結構
  • 單元素輸入
  • 大數值範圍

準備好解決了嗎?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在編輯器中開啟
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。