Le migliori 150 intervisteFacile

Intero inverso

Guida dettagliata e implementazione Python per il problema 'Reverse Integer'.

Dichiarazione del problema

Facile

Dato un intero con segno x a 32 bit, restituisce x con le cifre invertite. Se l'inversione di x fa sì che il valore esca dall'intervallo degli interi con segno a 32 bit [-2^31, 2^31 - 1], restituisce 0.

Supponiamo che l'ambiente non consenta di archiviare numeri interi a 64 bit (con o senza segno).

Scrivi una funzione reverse(x: int) -> int.

Vincoli
  • -2^31 <= x <= 2^31 - 1

Esempi

Example 1
Input
x = 123
Output
321
Explanation

Reversing 123 gives 321.

Example 2
Input
x = -123
Output
-321
Explanation

Reversing -123 gives -321.

Example 3
Input
x = 120
Output
21
Explanation

Reversing 120 gives 021, which is 21.

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.