簡単 0件解決済み 正答率0%

Pyramid Rows

Draw a right-aligned pyramid of # blocks.

このチャレンジで学べること

  • Build rows with padding and blocks
  • Compute spaces and symbols per row
  • Return lines as a list

問題

翻訳を表示

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

例

例 1
入力:
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.

例 2
入力:
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用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。