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

Hollow Diamond

Print the outline of a diamond: only the first and last star of each row.

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

  • Reuse the outline of a solid shape
  • Treat the width 1 row as a special case
  • Fill the inside of a row with spaces

โจทย์

ดูฉบับแปล

Take the solid diamond with 2n − 1 rows: number the rows k = 1 to 2n − 1, let m = n − |n − k|, and give row k n − m leading spaces and a block of width 2m − 1. In the hollow version only the two ends of each block are * and everything between them is a space. A block of width 1 is a single *.

Implement hollow_diamond(n).

Input

n: The number of rows in the top half, including the widest middle row.

Output

Return the 2n − 1 rows from top to bottom, without trailing spaces. When n is 0, return an empty list.

เงื่อนไข

0 ≤ n ≤ 100

ตัวอย่าง

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

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

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

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

คำอธิบาย

Widths are 1, 3, 5, 3, 1; the width 3 and 5 rows keep only their end stars: [" *", " * *", "* *", " * *", " *"].

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

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

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

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

คำอธิบาย

A single block of width 1: ["*"].

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

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

คำใบ้ 1

Work out the width of each row exactly as for the solid diamond.

คำใบ้ 2

A row of width w has a star, w − 2 spaces and a star.

คำใบ้ 3

Check what that rule does when w is 1 and handle that row separately.

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

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

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

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

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

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

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

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

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