आसान 0 हल किए गए 0% स्वीकृति

Floyd's Triangle

Build Floyd's triangle: the counting numbers written in rows of length 1, 2, 3 and so on.

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

  • Keep a counter that lives outside the inner loop
  • Join numbers with a separator
  • Build rows of growing length

समस्या

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

Floyd's triangle fills rows of growing length with consecutive integers starting at 1: row 1 holds 1, row 2 holds 2 3, row 3 holds 4 5 6, and so on. Row i (counting from 1) holds i numbers, and the first number of each row continues right after the last number of the row above.

Implement floyds_triangle(n).

Input

n: The number of rows.

Output

Return the rows from top to bottom. Inside a row, the numbers are written in decimal and separated by exactly one space, with no leading or trailing spaces.

सीमाएँ

0 ≤ n ≤ 100

उदाहरण

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

अपेक्षित आउटपुट:
expected = ["1", "2 3", "4 5 6", "7 8 9 10"]
इनपुट:
long long n = 4;

अपेक्षित आउटपुट:
std::vector<std::string> expected = {"1", "2 3", "4 5 6", "7 8 9 10"};
इनपुट:
long n = 4;

अपेक्षित आउटपुट:
string[] expected = new string[] {"1", "2 3", "4 5 6", "7 8 9 10"};

व्याख्या

The numbers 1 to 10 fill rows of length 1, 2, 3, 4: ["1", "2 3", "4 5 6", "7 8 9 10"].

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

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

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

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

व्याख्या

Only the first row: ["1"].

कैसे हल करें

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

संकेत 1

Keep one counter that starts at 1 and never resets between rows.

संकेत 2

Row i takes the next i values of that counter.

संकेत 3

Join the numbers with single spaces so nothing trails at the end.

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

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

संदर्भ

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

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

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

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

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

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