Le migliori 150 intervisteFacile

Numero unico

Guida dettagliata e implementazione Python per il problema del "numero singolo".

Dichiarazione del problema

Facile

Dato un array non vuoto di numeri interi, ogni elemento appare due volte tranne uno. Trova quell'unico.

È necessario implementare una soluzione con una complessità di runtime lineare e utilizzare solo spazio aggiuntivo costante.

Scrivi una funzione singleNumber(nums: List[int]) -> int.

Vincoli
  • 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

Esempi

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?
Prendi in considerazione l'utilizzo di strutture dati specifiche per la manipolazione dei bit come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

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

Apri nell'editor
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

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.