簡単 4件解決済み 正答率100.0%

Hollow Square

Draw the outline of a square with * characters.

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

  • Draw a border with characters
  • Detect first and last rows and columns
  • Build strings row by row

問題

翻訳を表示

Draw a hollow square of side size: the first and last rows are all *, and the rows in between have * only at the two ends with spaces inside.

Implement hollow_square(size).

Input

size: The side length.

Output

Return the rows from top to bottom.

制約

1 ≤ size ≤ 50

例

例 1
入力:
size = 4

期待される出力:
expected = ["****", "*  *", "*  *", "****"]
入力:
long long size = 4;

期待される出力:
std::vector<std::string> expected = {"****", "*  *", "*  *", "****"};
入力:
long size = 4;

期待される出力:
string[] expected = new string[] {"****", "*  *", "*  *", "****"};

解説

Rows are "****", "* *", "* *", "****".

例 2
入力:
size = 1

期待される出力:
expected = ["*"]
入力:
long long size = 1;

期待される出力:
std::vector<std::string> expected = {"*"};
入力:
long size = 1;

期待される出力:
string[] expected = new string[] {"*"};

解説

A 1×1 square is a single "*".

解き方のヒント

まずは自分で考えてから、ヒントを1つずつ開いてみましょう。

ヒント 1

The border is filled, while the inside stays empty.

ヒント 2

Treat the first and last rows differently from the middle rows.

ヒント 3

For size one, the single cell is both border and corner, so it must be filled.

目安の計算量

O(size^2) time, O(size^2) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

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

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

他の人の解答から学ぶ

コミュニティの解答

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