Условие
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. Следуйте условию и ограничениям выше.