โจทย์
The right triangle is usually the first pattern program in an intro course: row 1 has one *, row 2 has two, and so on down to row n. The outer loop picks the row and the inner loop (or string repetition) builds it.
Implement right_triangle_stars(n).
Input
n: The number of rows.
Output
Return the rows from top to bottom. Row i (counting from 1) is i * characters with no spaces.
เงื่อนไข
0 ≤ n ≤ 100
ตัวอย่าง
อินพุต:
n = 3
ผลลัพธ์ที่คาดหวัง:
expected = ["*", "**", "***"]
อินพุต:
long long n = 3;
ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"*", "**", "***"};
อินพุต:
long n = 3;
ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {"*", "**", "***"};
คำอธิบาย
Rows grow by one star: "*", "**", "***".
อินพุต:
n = 1
ผลลัพธ์ที่คาดหวัง:
expected = ["*"]
อินพุต:
long long n = 1;
ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"*"};
อินพุต:
long n = 1;
ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {"*"};
คำอธิบาย
A single row with one star: ["*"].
แนวทางการแก้
ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ
คำใบ้ 1
Row i needs exactly i stars, so the row number is all you need.
คำใบ้ 2
Repeat the star character instead of adding one character at a time if your language allows it.
คำใบ้ 3
When n is 0, return an empty list.
ความซับซ้อนที่คาดหวัง
O(n^2) time, O(n^2) extra space
แหล่งอ้างอิง
โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน