Problème
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.
Contraintes
-10¹⁸ ≤ n ≤ 10¹⁸
Exemples
Entrée :
n = 12321
Sortie attendue :
expected = True
Entrée :
long long n = 12321;
Sortie attendue :
bool expected = true;
Entrée :
long n = 12321;
Sortie attendue :
bool expected = true;
Explication
12321 reversed is 12321.
Entrée :
n = -121
Sortie attendue :
expected = False
Entrée :
long long n = -121;
Sortie attendue :
bool expected = false;
Entrée :
long n = -121;
Sortie attendue :
bool expected = false;
Explication
The minus sign makes "-121" differ from "121-".
Comment l'aborder
Essayez d'abord seul, puis ouvrez les indices un par un.
Indice 1
A negative number cannot be a palindrome because of the minus sign.
Indice 2
Compare the number’s digits from both ends rather than converting unnecessarily.
Indice 3
Single-digit numbers are always palindromes.
Complexité attendue
O(log n) time, O(1) extra space
Références
Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.