Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Alphabet Triangle

Print a right triangle of letters where row i spells the first i letters of the alphabet.

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

  • Turn a number into a letter by offsetting from 'A'
  • Build rows of growing length
  • Return a pattern as a list of rows

Đề bài

Xem bản dịch

Instead of stars, this right triangle uses the alphabet. Row i (counting from 1) holds the first i uppercase English letters in order, written next to each other with no spaces: row 1 is A, row 2 is AB, row 3 is ABC. Turning a position into a letter is a matter of adding it to the character code of A.

Implement alphabet_triangle(n).

Input

n: The number of rows (at most 26, one per letter).

Output

Return the rows from top to bottom.

Ràng buộc

0 ≤ n ≤ 26

Ví dụ

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

Kết quả mong đợi:
expected = ["A", "AB", "ABC", "ABCD"]
Đầu vào:
long long n = 4;

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

Kết quả mong đợi:
string[] expected = new string[] {"A", "AB", "ABC", "ABCD"};

Giải thích

Each row adds the next letter: ["A", "AB", "ABC", "ABCD"].

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

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

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

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

Giải thích

Only A: ["A"].

Hướng tiếp cận

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

Gợi ý 1

Position j in a row (from 0) is the letter j places after A.

Gợi ý 2

Most languages let you add an integer to a character code and turn it back into a character.

Gợi ý 3

Row i is just row i − 1 with one more letter.

Độ 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.