Le migliori 150 intervisteFacile

Numero mancante

Guida dettagliata e implementazione Python per il problema del "Numero mancante".

Dichiarazione del problema

Facile

Dato un array di numeri contenente n numeri distinti nell'intervallo [0, n], restituisce l'unico numero nell'intervallo che manca dall'array.

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

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

Esempi

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?
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.