ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Hollow Pyramid

Print the outline of a centered pyramid: its two slanted sides and a solid base.

สิ่งที่คุณจะได้เรียนรู้

  • Keep the outline of a centered shape
  • Treat the base and the tip as special rows
  • Avoid trailing spaces in output rows

โจทย์

ดูฉบับแปล

Row i (counting from 1) of a centered pyramid has n − i leading spaces and a block of width 2i − 1. In the hollow version the last row (the base) stays fully filled with stars, a block of width 1 is a single *, and every other block keeps only its two end stars with spaces between them.

Implement hollow_pyramid(n).

Input

n: The number of rows.

Output

Return the rows from top to bottom, without trailing spaces.

เงื่อนไข

0 ≤ n ≤ 100

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
n = 4

ผลลัพธ์ที่คาดหวัง:
expected = ["   *", "  * *", " *   *", "*******"]
อินพุต:
long long n = 4;

ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"   *", "  * *", " *   *", "*******"};
อินพุต:
long n = 4;

ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {"   *", "  * *", " *   *", "*******"};

คำอธิบาย

Rows 2 and 3 keep only their ends, row 4 is the solid base of 7 stars: [" *", " * *", " * *", "*******"].

ตัวอย่างที่ 2
อินพุต:
n = 2

ผลลัพธ์ที่คาดหวัง:
expected = [" *", "***"]
อินพุต:
long long n = 2;

ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {" *", "***"};
อินพุต:
long n = 2;

ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {" *", "***"};

คำอธิบาย

Row 2 is the base, so it is solid: [" *", "***"].

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

Leading spaces are the same as in the solid pyramid.

คำใบ้ 2

The tip has width 1 and the base is the last row; both are solid.

คำใบ้ 3

Any other row is a star, some spaces and a star.

ความซับซ้อนที่คาดหวัง

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

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก