兩個數字相加
「兩個數字相加」問題的詳細指南和 Python 實作。
1. 學習
「兩個數字相加」問題是鍊錶部分的關鍵挑戰。
此實作著重於 Python 中的簡單層級邏輯。
在我們提供的解決方案中,我們優先考慮技術準確性和程式碼可讀性。
2. Real-World Applications
3. Visual Intuition
可視化兩個數字相加的邏輯流程。
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
仔細閱讀兩個數字相加的問題陳述。
2. Formulate brute force
起草一個簡單的迭代解決方案。
3. Identify inefficiency
尋找冗餘計算。
4. Optimize search path
使用散列或排序來加速該過程。
5. Final Implementation
清理生產標準代碼。
問題陳述
給您兩個表示兩個非負整數的非空鍊錶。這些數字以相反的順序存儲,並且每個節點都包含一個數字。將兩個數字相加並以鍊錶形式傳回總和。
您可以假設這兩個數字不包含任何前導零,除了數字 0 本身。
連結列表表示為 Python 列表。實作一個函數 addTwoNumbers(l1: list, l2: list) -> list ,以相反的數字順序將總和傳回為列表。
- •The number of nodes in each linked list is in the range [1, 100]
- •0 <= Node.val <= 9
- •It is guaranteed that the list represents a number that does not have leading zeros
範例
[2,4,3], [5,6,4]
[7,0,8]
342 + 465 = 807. Represented in reverse: [7,0,8].
[0], [0]
[0]
0 + 0 = 0.
[9,9,9,9,9,9,9], [9,9,9,9]
[8,9,9,9,0,0,0,1]
9999999 + 9999 = 10009998. Represented in reverse: [8,9,9,9,0,0,0,1].
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.
面試見解和變化
複雜度分析分解
為什麼時間: Directly evaluates all possibilities.
為什麼選擇太空: Uses standard local memory.
為什麼時間: Optimized paths reduce total operations.
為什麼選擇太空: May trade memory for speed.
最佳化解決方案Python程式碼
最佳化解決方案Python程式碼
def add_two_numbers_opt(l1, l2):
if isinstance(l1, list):
node1 = build_linked_list(l1)
node2 = build_linked_list(l2)
res = add_two_numbers_opt_helper(node1, node2)
return linked_list_to_list(res)
return add_two_numbers_opt_helper(l1, l2)
def add_two_numbers_opt_helper(l1: ListNode, l2: ListNode) -> ListNode:
dummy = ListNode(0)
curr = dummy
carry = 0
while l1 or l2 or carry:
val1 = l1.val if l1 else 0
val2 = l2.val if l2 else 0
total = val1 + val2 + carry
carry = total // 10
curr.next = ListNode(total % 10)
curr = curr.next
if l1: l1 = l1.next
if l2: l2 = l2.next
return dummy.next暴力破解代碼(劇透保護)
暴力破解代碼(劇透保護)
def add_two_numbers_brute(l1, l2):
if isinstance(l1, list):
node1 = build_linked_list(l1)
node2 = build_linked_list(l2)
res = add_two_numbers_brute_helper(node1, node2)
return linked_list_to_list(res)
return add_two_numbers_brute_helper(l1, l2)
def add_two_numbers_brute_helper(l1: ListNode, l2: ListNode) -> ListNode:
def to_num(node):
num, place = 0, 1
while node:
num += node.val * place
place *= 10
node = node.next
return num
total = to_num(l1) + to_num(l2)
dummy = ListNode(0)
curr = dummy
for digit in str(total)[::-1]:
curr.next = ListNode(int(digit))
curr = curr.next
return dummy.next or ListNode(0)Algorithm Pattern Checklist
When dealing with Linked List data patterns.
- Are constraints clear?
- Is there a linear or logarithmic optimization possible?
PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!
推薦的 Python 資源
透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。
Python 迴圈:For 與 While 迴圈解釋
了解如何使用 Python 循環來迭代資料。透過互動式範例掌握 for 迴圈、while 迴圈、break、continue 和迴圈最佳實務。
如何在 Python 字典中新增和更新鍵
了解如何在 Python 字典中新增元素或更新鍵值對。探索方括號、更新方法和合併運算子選項。
Python 字串方法備忘單
Python 字串操作的完整參考指南。掌握格式化、搜尋、拆分、取代和檢查字串屬性。
Python 與 JavaScript:哪種程式語言最好?
Python 和 JavaScript 的全面比較。探索語法差異、效能、用例(後端與前端)和編碼範例。