मध्यम 0 हल किए गए 0% स्वीकृति

Number Diamond

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

आप क्या सीखेंगे

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

समस्या

अनुवाद दिखाएँ

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.

सीमाएँ

0 ≤ n ≤ 9

उदाहरण

उदाहरण 1
इनपुट:
n = 3

अपेक्षित आउटपुट:
expected = ["  1", " 121", "12321", " 121", "  1"]
इनपुट:
long long n = 3;

अपेक्षित आउटपुट:
std::vector<std::string> expected = {"  1", " 121", "12321", " 121", "  1"};
इनपुट:
long n = 3;

अपेक्षित आउटपुट:
string[] expected = new string[] {"  1", " 121", "12321", " 121", "  1"};

व्याख्या

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

उदाहरण 2
इनपुट:
n = 2

अपेक्षित आउटपुट:
expected = [" 1", "121", " 1"]
इनपुट:
long long n = 2;

अपेक्षित आउटपुट:
std::vector<std::string> expected = {" 1", "121", " 1"};
इनपुट:
long n = 2;

अपेक्षित आउटपुट:
string[] expected = new string[] {" 1", "121", " 1"};

व्याख्या

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

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

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

संकेत 2

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

संकेत 3

The leading spaces depend only on m.

अपेक्षित जटिलता

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

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

हर कोडर का सबसे अच्छा पास हुआ समाधान। तेज़, कम मेमोरी वाले और बेहतर रेटिंग वाले समाधान ऊपर रहते हैं।

अभी कोई पास हुआ समाधान नहीं है। इसे हल करें और पहला स्थान पाएँ।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।