Le migliori 150 intervisteFacile

Più uno

Guida dettagliata e implementazione Python per il problema "Plus One".

Dichiarazione del problema

Facile

Viene fornito un numero intero grande rappresentato come un array di cifre di interi, dove ciascuna cifra[i] è la i-esima cifra dell'intero. Le cifre sono ordinate dalla più significativa alla meno significativa da sinistra a destra. Il numero intero grande non contiene nessuno 0 iniziale.

Incrementa l'intero grande di uno e restituisce la matrice di cifre risultante.

Implementa una funzione plusOne(digits: list) -> list.

Vincoli
  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9
  • digits does not contain any leading 0's

Esempi

Example 1
Input
[1,2,3]
Output
[1,2,4]
Explanation

The array represents the integer 123. Incrementing by one gives 124.

Example 2
Input
[4,3,2,1]
Output
[4,3,2,2]
Explanation

The array represents the integer 4321. Incrementing by one gives 4322.

Example 3
Input
[9]
Output
[1,0]
Explanation

The array represents the integer 9. Incrementing by one gives 10, which is [1,0].

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche per matematica e geometria 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.