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

Number Triangle

Build a triangle of increasing numbers.

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

  • Build rows of growing length
  • Keep a counter across rows
  • Return a 2D list

โจทย์

ดูฉบับแปล

Build a number triangle: row i (counting from 1) lists the numbers 1, 2, …, i.

Implement number_triangle(rows).

Input

rows: The number of rows.

Output

Return the triangle as a list of rows.

เงื่อนไข

0 ≤ rows ≤ 30

ตัวอย่าง

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

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

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

ผลลัพธ์ที่คาดหวัง:
long[][] expected = new long[][] {
    new long[] {1},
    new long[] {1, 2},
    new long[] {1, 2, 3}
};

คำอธิบาย

Rows are [1], [1, 2] and [1, 2, 3].

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

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

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

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

คำอธิบาย

No rows were requested, so the triangle is empty.

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

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

คำใบ้ 1

Row i should contain the numbers from 1 up to i in order.

คำใบ้ 2

Grow each row by one element as you move downward.

คำใบ้ 3

When there are no rows, the result should simply be empty.

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

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

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

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

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

# นักโค้ด ภาษา เวลา หน่วยความจำ คะแนนจาก AI คำตอบ
1 KKKaneki Ken Python 656 ms - 5/5 ดูโค้ด
2 HJhettory Jurash Python 712 ms - 5/5 ดูโค้ด
3 LMLaura Mahlow Python 723 ms - 5/5 ดูโค้ด
4 ННикита Python 834 ms - 5/5 ดูโค้ด

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

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

สปอยล์: มีคำตอบเต็ม 4 ข้อด้านล่าง ลองแก้ด้วยตัวเองก่อน คุณยังสามารถดูโค้ดของนักพัฒนาคนอื่นและรีวิว AI ได้ เข้าสู่ระบบและแก้โจทย์เพื่อร่วมพูดคุย