Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Number Triangle

Build a triangle of increasing numbers.

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

0 ≤ rows ≤ 30

Ví dụ

Ví dụ 1
Đầu vào:
rows = 3

Kết quả mong đợi:
expected = [
    [1],
    [1, 2],
    [1, 2, 3]
]
Đầu vào:
long long rows = 3;

Kết quả mong đợi:
std::vector<std::vector<long long>> expected = {
    {1},
    {1, 2},
    {1, 2, 3}
};
Đầu vào:
long rows = 3;

Kết quả mong đợi:
long[][] expected = new long[][] {
    new long[] {1},
    new long[] {1, 2},
    new long[] {1, 2, 3}
};

Giải thích

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

Ví dụ 2
Đầu vào:
rows = 0

Kết quả mong đợi:
expected = []
Đầu vào:
long long rows = 0;

Kết quả mong đợi:
std::vector<std::vector<long long>> expected = {};
Đầu vào:
long rows = 0;

Kết quả mong đợi:
long[][] expected = new long[][] {};

Giải thích

No rows were requested, so the triangle is empty.

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

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

Gợi ý 2

Grow each row by one element as you move downward.

Gợi ý 3

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

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.