Problem
The first row is [1]. Each later row starts and ends with 1. Every interior entry is the sum of the two entries immediately above it. Return the first rows rows, beginning with the single 1.
Implement pascals_triangle(rows).
Input
rows: How many rows to generate.
Output
Return a list of rows, starting at the top; each row is a list of integers.
Einschränkungen
0 ≤ rows ≤ 60. Every entry fits a signed 64-bit integer. Zero rows returns an empty list.
Beispiele
Eingabe:
rows = 4
Erwartete Ausgabe:
expected = [
[1],
[1, 1],
[1, 2, 1],
[1, 3, 3, 1]
]
Eingabe:
long long rows = 4;
Erwartete Ausgabe:
std::vector<std::vector<long long>> expected = {
{1},
{1, 1},
{1, 2, 1},
{1, 3, 3, 1}
};
Eingabe:
long rows = 4;
Erwartete Ausgabe:
long[][] expected = new long[][] {
new long[] {1},
new long[] {1, 1},
new long[] {1, 2, 1},
new long[] {1, 3, 3, 1}
};
Erläuterung
The 2 in row 3 is 1 + 1. The two interior 3s in row 4 are 1 + 2 and 2 + 1.
Eingabe:
rows = 0
Erwartete Ausgabe:
expected = []
Eingabe:
long long rows = 0;
Erwartete Ausgabe:
std::vector<std::vector<long long>> expected = {};
Eingabe:
long rows = 0;
Erwartete Ausgabe:
long[][] expected = new long[][] {};
Erläuterung
No rows are requested, so return an empty list.
So gehst du vor
Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.
Hinweis 1
Each row starts and ends with 1, and interior values come from the two values above them.
Hinweis 2
Build rows one at a time from the previous row rather than recomputing from scratch.
Hinweis 3
Handle the zero-row case by returning an empty list immediately.
Erwartete Komplexität
O(rows^2) time, O(rows^2) extra space
Referenzen
Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.