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

Reverse a String

Return the characters of a string in reverse order.

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

  • Walk through a string backwards
  • Build a new string from characters
  • Use slicing or two pointers

โจทย์

ดูฉบับแปล

Reverse text so the last character comes first.

Implement reverse_string(text).

Input

text: The text to reverse.

Output

Return the reversed text.

เงื่อนไข

0 ≤ length ≤ 10,000

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
text = "hello"

ผลลัพธ์ที่คาดหวัง:
expected = "olleh"
อินพุต:
std::string text = "hello";

ผลลัพธ์ที่คาดหวัง:
std::string expected = "olleh";
อินพุต:
string text = "hello";

ผลลัพธ์ที่คาดหวัง:
string expected = "olleh";

คำอธิบาย

h-e-l-l-o read backwards is "olleh".

ตัวอย่างที่ 2
อินพุต:
text = "a"

ผลลัพธ์ที่คาดหวัง:
expected = "a"
อินพุต:
std::string text = "a";

ผลลัพธ์ที่คาดหวัง:
std::string expected = "a";
อินพุต:
string text = "a";

ผลลัพธ์ที่คาดหวัง:
string expected = "a";

คำอธิบาย

A single character reversed is itself.

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

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

คำใบ้ 1

The answer is the characters in opposite order.

คำใบ้ 2

A two-pointer approach or a backward traversal both work well.

คำใบ้ 3

Be sure the empty string stays empty after reversal.

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

O(n) time, O(n) extra space

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

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

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

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

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

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