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

Inverted Right Triangle

Print a right triangle upside down: n stars on the first row, one on the last.

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

  • Run a loop from n down to 1
  • Relate the row index to the number of symbols
  • Return a pattern as a list of rows

โจทย์

ดูฉบับแปล

Flip the right triangle: the first row is the longest, with n stars, and each following row has one star fewer, down to a single *. Counting the outer loop down instead of up is the whole trick.

Implement inverted_right_triangle(n).

Input

n: The number of rows.

Output

Return the rows from top to bottom. Row i (counting from 1) has n − i + 1 * characters.

เงื่อนไข

0 ≤ n ≤ 100

ตัวอย่าง

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

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

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

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

คำอธิบาย

Rows shrink by one star: "****", "***", "**", "*".

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

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

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

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

คำอธิบาย

With one row the triangle is just ["*"].

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

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

คำใบ้ 1

Let the outer loop count down from n to 1.

คำใบ้ 2

Each row has as many stars as the current loop value.

คำใบ้ 3

When n is 0, return an empty list.

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

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

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

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

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

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

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

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

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