150強訪談簡單

缺號碼

“缺失號碼”問題的詳細指南和 Python 實作。

問題陳述

簡單

給定一個包含 [0, n] 範圍內 n 個不同數字的陣列 nums,傳回該範圍內數組中唯一缺少的數字。

寫一個函數 missingNumber(nums: List[int]) -> int

約束條件
  • n == len(nums)
  • 1 <= n <= 10^4
  • 0 <= nums[i] <= n
  • All the numbers of nums are unique

範例

Example 1
Input
nums = [3,0,1]
Output
2
Explanation

n = 3, range [0,3]. 2 is missing.

Example 2
Input
nums = [0,1]
Output
2
Explanation

n = 2, range [0,2]. 2 is missing.

Example 3
Input
nums = [9,6,4,2,3,5,7,0,1]
Output
8
Explanation

n = 9, range [0,9]. 8 is missing.

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 資源

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