Tutte le sfide
Media 0 risolte 0% di accettazione

Double Staircase

Print two staircases of # blocks that face each other across a two-space gap.

Cosa imparerai

  • Right-align one part of a row and left-align another
  • Assemble a row from several pieces
  • Avoid trailing spaces in output rows

Problema

Mostra traduzione

Two staircases of height n stand back to back with a gap between them, like the facing pyramids in an old platform game. Row i (counting from 1) has n − i leading spaces, then i # characters, then exactly two spaces, then i more # characters. The left staircase is aligned to the right edge and the right one to the left edge.

Implement double_staircase(n).

Input

n: The height of each staircase.

Output

Return the rows from top to bottom. Every row ends with a #, so there are no trailing spaces.

Vincoli

0 ≤ n ≤ 50

Esempi

Esempio 1
Input:
n = 3

Output atteso:
expected = ["  #  #", " ##  ##", "###  ###"]
Input:
long long n = 3;

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

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

Spiegazione

Row 1 is 2 spaces, #, 2 spaces, #; row 3 has no leading spaces: [" # #", " ## ##", "### ###"].

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

One block on each side of the gap: ["# #"].

Come affrontarlo

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

Suggerimento 1

Build the left staircase first: it is a right-aligned triangle.

Suggerimento 2

The gap is always exactly two spaces.

Suggerimento 3

The right staircase has the same width as the left one on the same row and needs no padding after it.

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.