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

Reverse a Number

Reverse the decimal digits of a positive integer.

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

  • Take digits off the end of a number one by one
  • Build a new number by shifting digits left (× 10)
  • Handle trailing zeros correctly

โจทย์

ดูฉบับแปล

Write the decimal digits of n in reverse order and return the resulting number. Zeros that end up at the front disappear, so 1200 becomes 21.

Implement reverse_number(n).

Input

n: A positive integer.

Output

Return the reversed number.

เงื่อนไข

1 ≤ n ≤ 10¹⁸. The reversed value always fits a signed 64-bit integer.

ตัวอย่าง

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

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

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

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

คำอธิบาย

The digits 3, 5, 2 reversed are 2, 5, 3.

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

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

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

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

คำอธิบาย

Reversed digits are 0021; leading zeros are dropped, giving 21.

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

The key is to build the result one digit at a time from the end of the number.

คำใบ้ 2

Use repeated remainder and division to extract digits in reverse order.

คำใบ้ 3

Leading zeros in the reversed form should disappear naturally, so be careful not to preserve them.

ความซับซ้อนที่คาดหวัง

O(d) time, O(1) extra space

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

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

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

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

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