Mittel 0 gelöst 0% Erfolgsquote

Number Diamond

Print a diamond whose rows count up from 1 to a peak and back down.

Das lernst du

  • Combine the diamond shape with palindromic rows
  • Fold the top and bottom halves into one loop
  • Center rows with leading spaces

Problem

Übersetzung anzeigen

This diamond has 2n − 1 rows. Number the rows k = 1 to 2n − 1 and let m = n − |n − k|, so m goes 1, 2, …, n, …, 2, 1. Row k has n − m leading spaces, then the digits 1, 2, …, m, then m − 1, …, 1, written next to each other with no spaces. The middle row is the longest, for example 12321 when n = 3.

Implement number_diamond(n).

Input

n: The number of rows in the top half, including the middle row (at most 9, so every number is one digit).

Output

Return the 2n − 1 rows from top to bottom, without trailing spaces. When n is 0, return an empty list.

Einschränkungen

0 ≤ n ≤ 9

Beispiele

Beispiel 1
Eingabe:
n = 3

Erwartete Ausgabe:
expected = ["  1", " 121", "12321", " 121", "  1"]
Eingabe:
long long n = 3;

Erwartete Ausgabe:
std::vector<std::string> expected = {"  1", " 121", "12321", " 121", "  1"};
Eingabe:
long n = 3;

Erwartete Ausgabe:
string[] expected = new string[] {"  1", " 121", "12321", " 121", "  1"};

Erläuterung

m goes 1, 2, 3, 2, 1: [" 1", " 121", "12321", " 121", " 1"].

Beispiel 2
Eingabe:
n = 2

Erwartete Ausgabe:
expected = [" 1", "121", " 1"]
Eingabe:
long long n = 2;

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

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

Erläuterung

Three rows with m = 1, 2, 1: [" 1", "121", " 1"].

So gehst du vor

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

Hinweis 1

First work out the peak value m of each row: 1, 2, ..., n, ..., 2, 1.

Hinweis 2

A row with peak m counts from 1 up to m and back to 1.

Hinweis 3

The leading spaces depend only on m.

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.