Условие
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.
Ограничения
-10¹⁸ ≤ n ≤ 10¹⁸
Примеры
Ввод:
n = 12321
Ожидаемый вывод:
expected = True
Ввод:
long long n = 12321;
Ожидаемый вывод:
bool expected = true;
Ввод:
long n = 12321;
Ожидаемый вывод:
bool expected = true;
Пояснение
12321 reversed is 12321.
Ввод:
n = -121
Ожидаемый вывод:
expected = False
Ввод:
long long n = -121;
Ожидаемый вывод:
bool expected = false;
Ввод:
long n = -121;
Ожидаемый вывод:
bool expected = false;
Пояснение
The minus sign makes "-121" differ from "121-".
Как подступиться
Сначала попробуйте сами, затем открывайте подсказки по одной.
Подсказка 1
A negative number cannot be a palindrome because of the minus sign.
Подсказка 2
Compare the number’s digits from both ends rather than converting unnecessarily.
Подсказка 3
Single-digit numbers are always palindromes.
Ожидаемая сложность
O(log n) time, O(1) extra space
Ссылки
Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.