Đề bài
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.
Ràng buộc
0 ≤ n ≤ 100
Ví dụ
Đầu vào:
n = 4
Kết quả mong đợi:
expected = ["****", "***", "**", "*"]
Đầu vào:
long long n = 4;
Kết quả mong đợi:
std::vector<std::string> expected = {"****", "***", "**", "*"};
Đầu vào:
long n = 4;
Kết quả mong đợi:
string[] expected = new string[] {"****", "***", "**", "*"};
Giải thích
Rows shrink by one star: "****", "***", "**", "*".
Đầu vào:
n = 1
Kết quả mong đợi:
expected = ["*"]
Đầu vào:
long long n = 1;
Kết quả mong đợi:
std::vector<std::string> expected = {"*"};
Đầu vào:
long n = 1;
Kết quả mong đợi:
string[] expected = new string[] {"*"};
Giải thích
With one row the triangle is just ["*"].
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
Let the outer loop count down from n to 1.
Gợi ý 2
Each row has as many stars as the current loop value.
Gợi ý 3
When n is 0, return an empty list.
Độ phức tạp mong đợi
O(n^2) time, O(n^2) extra space
Tham khảo
Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.