Problema
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.
Vincoli
1 ≤ size ≤ 50
Esempi
Input:
size = 4
Output atteso:
expected = ["****", "* *", "* *", "****"]
Input:
long long size = 4;
Output atteso:
std::vector<std::string> expected = {"****", "* *", "* *", "****"};
Input:
long size = 4;
Output atteso:
string[] expected = new string[] {"****", "* *", "* *", "****"};
Spiegazione
Rows are "****", "* *", "* *", "****".
Input:
size = 1
Output atteso:
expected = ["*"]
Input:
long long size = 1;
Output atteso:
std::vector<std::string> expected = {"*"};
Input:
long size = 1;
Output atteso:
string[] expected = new string[] {"*"};
Spiegazione
A 1×1 square is a single "*".
Come affrontarlo
Prova prima da solo, poi apri un suggerimento alla volta.
Suggerimento 1
The border is filled, while the inside stays empty.
Suggerimento 2
Treat the first and last rows differently from the middle rows.
Suggerimento 3
For size one, the single cell is both border and corner, so it must be filled.
Complessità attesa
O(size^2) time, O(size^2) extra space
Riferimenti
Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.