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

Countdown to Liftoff

Count down from a number and end with Liftoff!

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

  • นับถอยหลังด้วยลูป
  • ผสมตัวเลขและข้อความในลิสต์
  • หยุดที่ค่าที่ถูกต้อง

โจทย์

Count down from start to 1, then add "Liftoff!". Each number becomes one text entry.

Implement countdown(start).

Input

start: The number to start counting down from.

Output

Return the countdown as a list of strings, ending with "Liftoff!".

เงื่อนไข

0 ≤ start ≤ 1,000

ตัวอย่าง

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

ผลลัพธ์ที่คาดหวัง:
expected = ["3", "2", "1", "Liftoff!"]
อินพุต:
long long start = 3;

ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"3", "2", "1", "Liftoff!"};
อินพุต:
long start = 3;

ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {"3", "2", "1", "Liftoff!"};

คำอธิบาย

"3", "2", "1", then "Liftoff!".

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

ผลลัพธ์ที่คาดหวัง:
expected = ["Liftoff!"]
อินพุต:
long long start = 0;

ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"Liftoff!"};
อินพุต:
long start = 0;

ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {"Liftoff!"};

คำอธิบาย

There is nothing to count, so only "Liftoff!" remains.

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

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

คำใบ้ 1

สร้างรายการโดยไล่ลงจากเลขเริ่มต้นจนถึง 1

คำใบ้ 2

สร้างแต่ละรายการเป็นข้อความ แล้วค่อยต่อข้อความอำลาสุดท้ายไว้ท้ายสุด

คำใบ้ 3

จำไว้ว่ากรณีอินพุตค่าน้อยสุดสามารถข้ามลูปตัวเลขได้ แต่ยังต้องคืนค่าแค่ข้อความอำลา

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

เวลา O(start), หน่วยความจำเพิ่ม O(start)

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

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

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

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

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