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

Right Triangle of Stars

Print the classic right triangle of stars: one star on the first row, n on the last.

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

  • Use the row number to decide how many symbols to draw
  • Build a row with an inner loop or string repetition
  • Return a pattern as a list of rows

โจทย์

ดูฉบับแปล

The right triangle is usually the first pattern program in an intro course: row 1 has one *, row 2 has two, and so on down to row n. The outer loop picks the row and the inner loop (or string repetition) builds it.

Implement right_triangle_stars(n).

Input

n: The number of rows.

Output

Return the rows from top to bottom. Row i (counting from 1) is i * characters with no spaces.

เงื่อนไข

0 ≤ n ≤ 100

ตัวอย่าง

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

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

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

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

คำอธิบาย

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

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

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

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

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

คำอธิบาย

A single row with one star: ["*"].

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

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

คำใบ้ 1

Row i needs exactly i stars, so the row number is all you need.

คำใบ้ 2

Repeat the star character instead of adding one character at a time if your language allows it.

คำใบ้ 3

When n is 0, return an empty list.

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

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

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

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

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

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

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

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

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