150強訪談簡單

單號

“單數”問題的詳細指南和 Python 實作。

問題陳述

簡單

給定一個非空整數數組 nums,除了一個之外,每個元素都出現兩次。找到那一個。

您必須實現具有線性運行時複雜度的解決方案,並且僅使用恆定的額外空間。

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

約束條件
  • 1 <= len(nums) <= 3 * 10^4
  • -3 * 10^4 <= nums[i] <= 3 * 10^4
  • Each element in the array appears twice except for one element which appears only once

範例

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

The element 1 appears once.

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

The element 4 appears once.

Example 3
Input
nums = [1]
Output
1
Explanation

The element 1 appears once.

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

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