Tutte le sfide
Facile 0 risolte 0% di accettazione

Number Triangle

Build a triangle of increasing numbers.

Cosa imparerai

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

Problema

Mostra traduzione

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.

Vincoli

0 ≤ rows ≤ 30

Esempi

Esempio 1
Input:
rows = 3

Output atteso:
expected = [
    [1],
    [1, 2],
    [1, 2, 3]
]
Input:
long long rows = 3;

Output atteso:
std::vector<std::vector<long long>> expected = {
    {1},
    {1, 2},
    {1, 2, 3}
};
Input:
long rows = 3;

Output atteso:
long[][] expected = new long[][] {
    new long[] {1},
    new long[] {1, 2},
    new long[] {1, 2, 3}
};

Spiegazione

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

Esempio 2
Input:
rows = 0

Output atteso:
expected = []
Input:
long long rows = 0;

Output atteso:
std::vector<std::vector<long long>> expected = {};
Input:
long rows = 0;

Output atteso:
long[][] expected = new long[][] {};

Spiegazione

No rows were requested, so the triangle is empty.

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

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

Suggerimento 2

Grow each row by one element as you move downward.

Suggerimento 3

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

Complessità attesa

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

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.