Условие
Reverse text so the last character comes first.
Implement reverse_string(text).
Input
text: The text to reverse.
Output
Return the reversed text.
Ограничения
0 ≤ length ≤ 10,000
Примеры
Ввод:
text = "hello"
Ожидаемый вывод:
expected = "olleh"
Ввод:
std::string text = "hello";
Ожидаемый вывод:
std::string expected = "olleh";
Ввод:
string text = "hello";
Ожидаемый вывод:
string expected = "olleh";
Пояснение
h-e-l-l-o read backwards is "olleh".
Ввод:
text = "a"
Ожидаемый вывод:
expected = "a"
Ввод:
std::string text = "a";
Ожидаемый вывод:
std::string expected = "a";
Ввод:
string text = "a";
Ожидаемый вывод:
string expected = "a";
Пояснение
A single character reversed is itself.
Как подступиться
Сначала попробуйте сами, затем открывайте подсказки по одной.
Подсказка 1
The answer is the characters in opposite order.
Подсказка 2
A two-pointer approach or a backward traversal both work well.
Подсказка 3
Be sure the empty string stays empty after reversal.
Ожидаемая сложность
O(n) time, O(n) extra space
Ссылки
Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.