โจทย์
Flip the right triangle: the first row is the longest, with n stars, and each following row has one star fewer, down to a single *. Counting the outer loop down instead of up is the whole trick.
Implement inverted_right_triangle(n).
Input
n: The number of rows.
Output
Return the rows from top to bottom. Row i (counting from 1) has n − i + 1 * characters.
เงื่อนไข
0 ≤ n ≤ 100
ตัวอย่าง
อินพุต:
n = 4
ผลลัพธ์ที่คาดหวัง:
expected = ["****", "***", "**", "*"]
อินพุต:
long long n = 4;
ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"****", "***", "**", "*"};
อินพุต:
long n = 4;
ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {"****", "***", "**", "*"};
คำอธิบาย
Rows shrink by one star: "****", "***", "**", "*".
อินพุต:
n = 1
ผลลัพธ์ที่คาดหวัง:
expected = ["*"]
อินพุต:
long long n = 1;
ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"*"};
อินพุต:
long n = 1;
ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {"*"};
คำอธิบาย
With one row the triangle is just ["*"].
แนวทางการแก้
ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ
คำใบ้ 1
Let the outer loop count down from n to 1.
คำใบ้ 2
Each row has as many stars as the current loop value.
คำใบ้ 3
When n is 0, return an empty list.
ความซับซ้อนที่คาดหวัง
O(n^2) time, O(n^2) extra space
แหล่งอ้างอิง
โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน