Tutte le sfide
Facile 0 risolte 0% di accettazione

Inverted Right Triangle

Print a right triangle upside down: n stars on the first row, one on the last.

Cosa imparerai

  • Run a loop from n down to 1
  • Relate the row index to the number of symbols
  • Return a pattern as a list of rows

Problema

Mostra traduzione

Flip the right triangle: the first row is the longest, with n stars, and each following row has one star fewer, down to a single *. Counting the outer loop down instead of up is the whole trick.

Implement inverted_right_triangle(n).

Input

n: The number of rows.

Output

Return the rows from top to bottom. Row i (counting from 1) has n − i + 1 * characters.

Vincoli

0 ≤ n ≤ 100

Esempi

Esempio 1
Input:
n = 4

Output atteso:
expected = ["****", "***", "**", "*"]
Input:
long long n = 4;

Output atteso:
std::vector<std::string> expected = {"****", "***", "**", "*"};
Input:
long n = 4;

Output atteso:
string[] expected = new string[] {"****", "***", "**", "*"};

Spiegazione

Rows shrink by one star: "****", "***", "**", "*".

Esempio 2
Input:
n = 1

Output atteso:
expected = ["*"]
Input:
long long n = 1;

Output atteso:
std::vector<std::string> expected = {"*"};
Input:
long n = 1;

Output atteso:
string[] expected = new string[] {"*"};

Spiegazione

With one row the triangle is just ["*"].

Come affrontarlo

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

Suggerimento 1

Let the outer loop count down from n to 1.

Suggerimento 2

Each row has as many stars as the current loop value.

Suggerimento 3

When n is 0, return an empty list.

Complessità attesa

O(n^2) time, O(n^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.

Ancora nessuna soluzione superata. Risolvila e prendi il primo posto.

Impara dagli altri

Soluzioni della community

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