Đề bài
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.
Ràng buộc
-10¹⁸ ≤ n ≤ 10¹⁸
Ví dụ
Đầu vào:
n = 12321
Kết quả mong đợi:
expected = True
Đầu vào:
long long n = 12321;
Kết quả mong đợi:
bool expected = true;
Đầu vào:
long n = 12321;
Kết quả mong đợi:
bool expected = true;
Giải thích
12321 reversed is 12321.
Đầu vào:
n = -121
Kết quả mong đợi:
expected = False
Đầu vào:
long long n = -121;
Kết quả mong đợi:
bool expected = false;
Đầu vào:
long n = -121;
Kết quả mong đợi:
bool expected = false;
Giải thích
The minus sign makes "-121" differ from "121-".
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
A negative number cannot be a palindrome because of the minus sign.
Gợi ý 2
Compare the number’s digits from both ends rather than converting unnecessarily.
Gợi ý 3
Single-digit numbers are always palindromes.
Độ phức tạp mong đợi
O(log n) time, O(1) extra space
Tham khảo
Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.