簡単 4件解決済み 正答率57.1%

Number Triangle

Build a triangle of increasing numbers.

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

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

問題

翻訳を表示

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.

制約

0 ≤ rows ≤ 30

例

例 1
入力:
rows = 3

期待される出力:
expected = [
    [1],
    [1, 2],
    [1, 2, 3]
]
入力:
long long rows = 3;

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

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

解説

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

例 2
入力:
rows = 0

期待される出力:
expected = []
入力:
long long rows = 0;

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

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

解説

No rows were requested, so the triangle is empty.

解き方のヒント

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

ヒント 1

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

ヒント 2

Grow each row by one element as you move downward.

ヒント 3

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

目安の計算量

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

参考資料

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

ランキング

全体ランキング

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

# コーダー 言語 時間 メモリ AI 評価 解答
1 KKKaneki Ken Python 656 ms - 5/5 コードを見る
2 HJhettory Jurash Python 712 ms - 5/5 コードを見る
3 LMLaura Mahlow Python 723 ms - 5/5 コードを見る
4 Н"Никита "Programmer" Python 834 ms - 5/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

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