Problema
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
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.
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.