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

Butterfly Pattern

Print a butterfly: two star triangles facing each other that grow until they touch, then shrink again.

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

  • Build a row from three parts: left wing, gap and right wing
  • Mirror a half to get the full shape
  • Spot the row where the gap disappears

โจทย์

ดูฉบับแปล

The butterfly has 2n rows. In the top half, row i (i = 1 to n) is i stars, then 2(n − i) spaces, then i stars again, so the two wings meet in the middle on row n. The bottom half repeats the same rows in reverse order, from i = n back down to i = 1, so the widest row appears twice in the middle.

Implement butterfly_pattern(n).

Input

n: The number of rows in each half.

Output

Return the 2n rows from top to bottom. Every row ends with a star, so no row has trailing spaces. When n is 0, return an empty list.

เงื่อนไข

0 ≤ n ≤ 50

ตัวอย่าง

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

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

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

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

คำอธิบาย

Top half: "* *", "** **", "******"; the bottom half is the same three rows reversed.

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

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

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

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

คำอธิบาย

Each half is one row of 1 + 0 + 1 characters: ["**", "**"].

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

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

คำใบ้ 1

In the top half, row i has i stars on each side.

คำใบ้ 2

The gap shrinks by two each row and is empty on row n.

คำใบ้ 3

The bottom half is the top half in reverse, including row n again.

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

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

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

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

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

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

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

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

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