150強訪談簡單

包含重複項

“包含重複”問題的詳細指南和 Python 實作。

問題陳述

簡單

給定一個整數陣列 nums,如果任何值在陣列中至少出現兩次,則傳回 True;如果每個元素都不同,則傳回 False

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

約束條件
  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9

範例

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

The element 1 appears at index 0 and index 3, so there is a duplicate.

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

All elements are distinct.

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

Multiple elements repeat: 1, 3, 4, and 2.

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

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