Python Nozioni di baseFacile

Trovare il palindromo più lungo in un array

Guida dettagliata e implementazione Python per il problema "Trovare il palindromo più lungo in un array".

Dichiarazione del problema

Facile

Scrivi una funzione longest_palindrome(arr) che prenda un elenco di interi positivi e restituisca il numero più grande nell'array le cui cifre formano un palindromo. Un numero palindromo si legge allo stesso modo sia in avanti che all'indietro (ad esempio, 121, 1331, 7). Se non esiste alcun numero palindromo, restituisce -1.

Vincoli
  • 1 <= len(arr) <= 10^5
  • 1 <= arr[i] <= 10^9

Esempi

Example 1
Input
arr = [12, 121, 33, 456, 1331]
Output
1331
Explanation

Palindromic numbers: 121, 33, 1331. The largest is 1331.

Example 2
Input
arr = [10, 20, 30]
Output
-1
Explanation

None of 10, 20, 30 are palindromes (10 reversed is 01 which is 1, not 10).

Example 3
Input
arr = [7, 11, 22, 123]
Output
22
Explanation

Palindromic numbers: 7, 11, 22. The largest is 22.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche degli array 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.