Đề bài
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ụ
Đầ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].
Đầ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.