Last Updated: June 7, 2026
A number pyramid is a stack of rows where each row counts one further than the one above it. Row 1 is 1, row 2 counts 1 and 2 to give 12, row 3 counts up to 3 to give 123. Each step down adds the next digit to the end.
The output is a list of strings, one per row. Row i is the digits from 1 up to i with nothing in between. For n = 3 the answer is ["1", "12", "123"]. For n = 1 it is ["1"].
Because the digits sit directly next to each other, n is capped so every digit stays a single character. That keeps "123" meaning the digits 1, 2, 3 rather than a larger number.
1 <= n <= 9 → n is always at least 1, so there is always at least one row. The cap at 9 keeps each digit a single character, so a row never mixes single and multi-digit numbers.i holds the digits 1 through i → The length of row i is exactly i characters. The last row has n characters, and the rows grow by one character each step down.Each row is built by counting from 1 up to the row's number, which is a loop inside a loop. The outer loop walks the rows from 1 to n, and for each row the inner loop counts from 1 up to that row's index, appending every digit.
The outer index i does double duty: it names the row and sets how far the inner loop counts. Row i counts up to i, so the inner loop runs while its counter is at most i.
result to hold the rows.i from 1 to n:j from 1 to i, append the digit j to the string.result.result.Input:
For row 1 the inner loop runs only with j = 1, producing "1". For row 2 it runs j = 1 then j = 2, producing "12". For row 3 it counts through 1, 2, 3 to give "123". For row 4 it counts to 4, giving "1234". Each row's string is collected in turn:
i appends i characters, so the total work across all rows is 1 + 2 + ... + n, which grows with the square of n.1 + 2 + ... + n characters, which is on the order of n².The inner loop rebuilds the full count from 1 for every single row, even though each row only adds one digit to the row above it. Can we reuse the previous row instead of counting from the start each time?
Row 3 is "123" and row 4 is "1234", differing only by the extra 4. Every row is the row above it with one more digit appended.
So keep the text of the current row, and on each step append the next digit before recording it. There is no need to recount from 1, because the running string already holds everything up to the previous digit.
result and an empty string current.i from 1 to n:i to current.current to result as the row for this step.result.Input:
The running string starts empty. On step 1, appending 1 makes it "1", which becomes row 1. On step 2, appending 2 makes it "12", row 2. Step 3 appends 3 to give "123", and step 4 appends 4 to give "1234". Each row is recorded right after its digit is added:
n times, but copying the growing string into the result on each step costs up to n characters, so the totals still add up to the order of n².1 + 2 + ... + n characters in all. The single running string reuses its content but does not change this order.