問題
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 つずつ開いてみましょう。
ヒント 1
Each row combines leading spaces with a growing run of hash characters.
ヒント 2
Build rows one by one from top to bottom so the alignment stays correct.
ヒント 3
If the height is zero, return no rows rather than an empty-looking pyramid.
目安の計算量
O(height^2) time, O(height^2) extra space
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。