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

X Pattern

Draw both diagonals of an n by n square with stars to form an X.

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

  • Recognise the two diagonals of a square by their coordinates
  • Build each row cell by cell
  • Trim trailing spaces from a row

โจทย์

ดูฉบับแปล

Picture an n by n grid with rows and columns numbered from 0. The cell in row r and column c holds * when it lies on either diagonal, that is when c = r or c = n − 1 − r; every other cell is a space. Then each row has its trailing spaces removed. For odd n the diagonals cross in a single centre cell; for even n they pass between cells.

Implement x_pattern(n).

Input

n: The side length of the square.

Output

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

เงื่อนไข

0 ≤ n ≤ 100

ตัวอย่าง

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

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

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

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

คำอธิบาย

Row 0 has stars at columns 0 and 4, row 2 only at column 2: ["* *", " * *", " *", " * *", "* *"].

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

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

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

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

คำอธิบาย

Row 1 has stars at columns 1 and 2, which sit side by side: ["* *", " **", " **", "* *"].

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

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

คำใบ้ 1

The main diagonal is where the row equals the column.

คำใบ้ 2

The other diagonal is where the row and column add up to n − 1.

คำใบ้ 3

Remove trailing spaces after building each row.

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

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

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

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

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

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

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

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

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