Problème
Build an n × n multiplication table: the value in row i, column j (both counting from 1) is i × j.
Implement multiplication_grid(n).
Input
n: The table size.
Output
Return the table as a list of rows.
Contraintes
0 ≤ n ≤ 20
Exemples
Entrée :
n = 3
Sortie attendue :
expected = [
[1, 2, 3],
[2, 4, 6],
[3, 6, 9]
]
Entrée :
long long n = 3;
Sortie attendue :
std::vector<std::vector<long long>> expected = {
{1, 2, 3},
{2, 4, 6},
{3, 6, 9}
};
Entrée :
long n = 3;
Sortie attendue :
long[][] expected = new long[][] {
new long[] {1, 2, 3},
new long[] {2, 4, 6},
new long[] {3, 6, 9}
};
Explication
Rows are [1, 2, 3], [2, 4, 6] and [3, 6, 9].
Entrée :
n = 1
Sortie attendue :
expected = [
[1]
]
Entrée :
long long n = 1;
Sortie attendue :
std::vector<std::vector<long long>> expected = {
{1}
};
Entrée :
long n = 1;
Sortie attendue :
long[][] expected = new long[][] {
new long[] {1}
};
Explication
A 1×1 table holds only 1 × 1 = 1.
Comment l'aborder
Essayez d'abord seul, puis ouvrez les indices un par un.
Indice 1
Every cell is just the product of its row number and column number.
Indice 2
Construct the table row by row, filling each row left to right.
Indice 3
Handle the zero-size case by returning an empty table, not a single empty row.
Complexité attendue
O(n^2) time, O(n^2) extra space
Références
Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.