Tutte le sfide
Facile 2 risolte 66.7% di accettazione

Palindrome Number

Check whether a number reads the same backwards.

Cosa imparerai

  • Reverse a number digit by digit
  • Compare the reversed value with the original
  • Handle negative numbers and trailing zeros

Problema

Mostra traduzione

Decide whether the decimal digits of n read the same forwards and backwards. Negative numbers are never palindromes because of the minus sign.

Implement is_palindrome_number(n).

Input

n: The number to check.

Output

Return true for a palindromic number, otherwise false.

Vincoli

-10¹⁸ ≤ n ≤ 10¹⁸

Esempi

Esempio 1
Input:
n = 12321

Output atteso:
expected = True
Input:
long long n = 12321;

Output atteso:
bool expected = true;
Input:
long n = 12321;

Output atteso:
bool expected = true;

Spiegazione

12321 reversed is 12321.

Esempio 2
Input:
n = -121

Output atteso:
expected = False
Input:
long long n = -121;

Output atteso:
bool expected = false;
Input:
long n = -121;

Output atteso:
bool expected = false;

Spiegazione

The minus sign makes "-121" differ from "121-".

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

A negative number cannot be a palindrome because of the minus sign.

Suggerimento 2

Compare the number’s digits from both ends rather than converting unnecessarily.

Suggerimento 3

Single-digit numbers are always palindromes.

Complessità attesa

O(log n) time, O(1) extra space

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

# Programmatore Linguaggio Tempo Memoria Voto dell'IA Soluzione
1 KKKaneki Ken Python 965 ms - 5/5 Vedi il codice
2 HJhettory Jurash Python 980 ms - 4/5 Vedi il codice

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 2 soluzioni complete sotto Prova prima a risolverla da solo. Puoi comunque sbirciare il codice degli altri sviluppatori e le recensioni AI. Accedi e risolvi la sfida per partecipare alla discussione.