Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Palindrome Number

Check whether a number reads the same backwards.

Bạn sẽ học được gì

  • Reverse a number digit by digit
  • Compare the reversed value with the original
  • Handle negative numbers and trailing zeros

Đề bài

Xem bản dịch

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ụ

Ví dụ 1
Đầ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.

Ví dụ 2
Đầ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.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.