โจทย์
Draw a right-aligned pyramid like the one in Super Mario: row i (counting from 1) has height − i spaces followed by i # characters.
Implement pyramid_rows(height).
Input
height: The number of rows.
Output
Return the rows from top to bottom.
เงื่อนไข
0 ≤ height ≤ 50
ตัวอย่าง
อินพุต:
height = 3
ผลลัพธ์ที่คาดหวัง:
expected = [" #", " ##", "###"]
อินพุต:
long long height = 3;
ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {" #", " ##", "###"};
อินพุต:
long height = 3;
ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {" #", " ##", "###"};
คำอธิบาย
Rows are " #", " ##" and "###": each row has one more block and one less space.
อินพุต:
height = 1
ผลลัพธ์ที่คาดหวัง:
expected = ["#"]
อินพุต:
long long height = 1;
ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"#"};
อินพุต:
long height = 1;
ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {"#"};
คำอธิบาย
A single row with one block: "#".
แนวทางการแก้
ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ
คำใบ้ 1
แต่ละแถวประกอบด้วยช่องว่างนำหน้าและชุดของเครื่องหมาย # ที่เพิ่มขึ้น
คำใบ้ 2
สร้างทีละแถวจากบนลงล่างเพื่อให้การจัดแนวยังถูกต้อง
คำใบ้ 3
ถ้าความสูงเป็น 0 ให้คืนค่าเป็นไม่มีแถว แทนที่จะเป็นพีระมิดว่าง ๆ
ความซับซ้อนที่คาดหวัง
เวลา O(height^2), หน่วยความจำเพิ่ม O(height^2)
แหล่งอ้างอิง
โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน