Torna alle sfide
Facile 0 risolte 0% 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.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.