ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Palindrome Number

Check whether a number reads the same backwards.

สิ่งที่คุณจะได้เรียนรู้

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

โจทย์

ดูฉบับแปล

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¹⁸

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
n = 12321

ผลลัพธ์ที่คาดหวัง:
expected = True
อินพุต:
long long n = 12321;

ผลลัพธ์ที่คาดหวัง:
bool expected = true;
อินพุต:
long n = 12321;

ผลลัพธ์ที่คาดหวัง:
bool expected = true;

คำอธิบาย

12321 reversed is 12321.

ตัวอย่างที่ 2
อินพุต:
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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก