Tutte le sfide
Facile 4 risolte 57.1% 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.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

# Programmatore Linguaggio Tempo Memoria Voto dell'IA Soluzione
1 KKKaneki Ken Python 656 ms - 5/5 Vedi il codice
2 HJhettory Jurash Python 712 ms - 5/5 Vedi il codice
3 LMLaura Mahlow Python 723 ms - 5/5 Vedi il codice
4 ННикита Python 834 ms - 5/5 Vedi il codice

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 4 soluzioni complete sotto Prova prima a risolverla da solo. Puoi comunque sbirciare il codice degli altri sviluppatori e le recensioni AI. Accedi e risolvi la sfida per partecipare alla discussione.