Đề bài
Reverse text so the last character comes first.
Implement reverse_string(text).
Input
text: The text to reverse.
Output
Return the reversed text.
Ràng buộc
0 ≤ length ≤ 10,000
Ví dụ
Đầu vào:
text = "hello"
Kết quả mong đợi:
expected = "olleh"
Đầu vào:
std::string text = "hello";
Kết quả mong đợi:
std::string expected = "olleh";
Đầu vào:
string text = "hello";
Kết quả mong đợi:
string expected = "olleh";
Giải thích
h-e-l-l-o read backwards is "olleh".
Đầu vào:
text = "a"
Kết quả mong đợi:
expected = "a"
Đầu vào:
std::string text = "a";
Kết quả mong đợi:
std::string expected = "a";
Đầu vào:
string text = "a";
Kết quả mong đợi:
string expected = "a";
Giải thích
A single character reversed is itself.
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
The answer is the characters in opposite order.
Gợi ý 2
A two-pointer approach or a backward traversal both work well.
Gợi ý 3
Be sure the empty string stays empty after reversal.
Độ phức tạp mong đợi
O(n) time, O(n) 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.