Alle Herausforderungen
Einfach 0 gelöst 0% Erfolgsquote

Inverted Right Triangle

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

Das lernst du

  • 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

Problem

Übersetzung anzeigen

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.

Einschränkungen

0 ≤ n ≤ 100

Beispiele

Beispiel 1
Eingabe:
n = 4

Erwartete Ausgabe:
expected = ["****", "***", "**", "*"]
Eingabe:
long long n = 4;

Erwartete Ausgabe:
std::vector<std::string> expected = {"****", "***", "**", "*"};
Eingabe:
long n = 4;

Erwartete Ausgabe:
string[] expected = new string[] {"****", "***", "**", "*"};

Erläuterung

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

Beispiel 2
Eingabe:
n = 1

Erwartete Ausgabe:
expected = ["*"]
Eingabe:
long long n = 1;

Erwartete Ausgabe:
std::vector<std::string> expected = {"*"};
Eingabe:
long n = 1;

Erwartete Ausgabe:
string[] expected = new string[] {"*"};

Erläuterung

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

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

Let the outer loop count down from n to 1.

Hinweis 2

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

Hinweis 3

When n is 0, return an empty list.

Erwartete Komplexität

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

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Die beste bestandene Lösung jedes Coders. Schnellere, sparsamere und besser bewertete Lösungen stehen weiter oben.

Noch keine bestandene Lösung. Löse sie und hol dir Platz eins.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.