Đề bài
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ụ
Đầ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"].
Đầ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.