Tất cả thử thách
Trung bình 0 đã giải Tỷ lệ chấp nhận 0%

Palindromic Number Pyramid

Print a centered pyramid where each row counts up to its row number and back down.

Bạn sẽ học được gì

  • Build a row from an ascending and a descending run
  • Center rows with leading spaces
  • Recognise a palindrome by its mirror symmetry

Đề bài

Xem bản dịch

Each row of this pyramid reads the same forwards and backwards. Row i (counting from 1) has n − i leading spaces, then the digits 1, 2, …, i, then i − 1, …, 2, 1, all written next to each other with no spaces in between. So row 3 is 12321 and row 1 is 1.

Implement palindromic_number_pyramid(n).

Input

n: The number of rows (at most 9, so every number is a single digit).

Output

Return the rows from top to bottom, without trailing spaces.

Ràng buộc

0 ≤ n ≤ 9

Ví dụ

Ví dụ 1
Đầu vào:
n = 3

Kết quả mong đợi:
expected = ["  1", " 121", "12321"]
Đầu vào:
long long n = 3;

Kết quả mong đợi:
std::vector<std::string> expected = {"  1", " 121", "12321"};
Đầu vào:
long n = 3;

Kết quả mong đợi:
string[] expected = new string[] {"  1", " 121", "12321"};

Giải thích

Row 1 is 2 spaces and 1, row 2 is 1 space and 121, row 3 is 12321: [" 1", " 121", "12321"].

Ví dụ 2
Đầu vào:
n = 1

Kết quả mong đợi:
expected = ["1"]
Đầu vào:
long long n = 1;

Kết quả mong đợi:
std::vector<std::string> expected = {"1"};
Đầu vào:
long n = 1;

Kết quả mong đợi:
string[] expected = new string[] {"1"};

Giải thích

One row with the digit 1: ["1"].

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

Split each row into an up part 1..i and a down part i − 1..1.

Gợi ý 2

The down part is the up part reversed without its last digit.

Gợi ý 3

Leading spaces follow the same rule as in the star pyramid.

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

Chưa có lời giải nào đạt. Giải bài này để giành hạng nhất.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.