簡単 2件解決済み 正答率100.0%

Multiplication Grid

Build a full multiplication table.

このチャレンジで学べること

  • Build a table with two loops
  • Create a list of lists
  • Index rows and columns

問題

翻訳を表示

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.

制約

0 ≤ n ≤ 20

例

例 1
入力:
n = 3

期待される出力:
expected = [
    [1, 2, 3],
    [2, 4, 6],
    [3, 6, 9]
]
入力:
long long n = 3;

期待される出力:
std::vector<std::vector<long long>> expected = {
    {1, 2, 3},
    {2, 4, 6},
    {3, 6, 9}
};
入力:
long n = 3;

期待される出力:
long[][] expected = new long[][] {
    new long[] {1, 2, 3},
    new long[] {2, 4, 6},
    new long[] {3, 6, 9}
};

解説

Rows are [1, 2, 3], [2, 4, 6] and [3, 6, 9].

例 2
入力:
n = 1

期待される出力:
expected = [
    [1]
]
入力:
long long n = 1;

期待される出力:
std::vector<std::vector<long long>> expected = {
    {1}
};
入力:
long n = 1;

期待される出力:
long[][] expected = new long[][] {
    new long[] {1}
};

解説

A 1×1 table holds only 1 × 1 = 1.

解き方のヒント

まずは自分で考えてから、ヒントを1つずつ開いてみましょう。

ヒント 1

Every cell is just the product of its row number and column number.

ヒント 2

Construct the table row by row, filling each row left to right.

ヒント 3

Handle the zero-size case by returning an empty table, not a single empty row.

目安の計算量

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

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 HJhettory Jurash Python 827 ms - 5/5 コードを見る
2 LMLaura Mahlow Python 855 ms - 4/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意: この下に解答が2件あります まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。