簡単 3件解決済み 正答率100.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用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 HJhettory Jurash Python 778 ms - 4/5 コードを見る
2 LMLaura Mahlow Python 814 ms - 4/5 コードを見る
3 KKKaneki Ken Python 849 ms - 5/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意:下に3件の全解答あり まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。