Đề bài
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.
Ràng buộc
1 ≤ size ≤ 50
Ví dụ
Đầu vào:
size = 4
Kết quả mong đợi:
expected = ["****", "* *", "* *", "****"]
Đầu vào:
long long size = 4;
Kết quả mong đợi:
std::vector<std::string> expected = {"****", "* *", "* *", "****"};
Đầu vào:
long size = 4;
Kết quả mong đợi:
string[] expected = new string[] {"****", "* *", "* *", "****"};
Giải thích
Rows are "****", "* *", "* *", "****".
Đầu vào:
size = 1
Kết quả mong đợi:
expected = ["*"]
Đầu vào:
long long size = 1;
Kết quả mong đợi:
std::vector<std::string> expected = {"*"};
Đầu vào:
long size = 1;
Kết quả mong đợi:
string[] expected = new string[] {"*"};
Giải thích
A 1×1 square is a single "*".
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
The border is filled, while the inside stays empty.
Gợi ý 2
Treat the first and last rows differently from the middle rows.
Gợi ý 3
For size one, the single cell is both border and corner, so it must be filled.
Độ phức tạp mong đợi
O(size^2) time, O(size^2) extra space
Tham khảo
Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.