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

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

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

# นักโค้ด ภาษา เวลา หน่วยความจำ คะแนนจาก AI คำตอบ
1 KKKaneki Ken Python 965 ms - 5/5 ดูโค้ด
2 HJhettory Jurash Python 980 ms - 4/5 ดูโค้ด

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

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

สปอยล์: มีคำตอบเต็ม 2 ข้อด้านล่าง ลองแก้ด้วยตัวเองก่อน คุณยังสามารถดูโค้ดของนักพัฒนาคนอื่นและรีวิว AI ได้ เข้าสู่ระบบและแก้โจทย์เพื่อร่วมพูดคุย