Problème
Reverse text so the last character comes first.
Implement reverse_string(text).
Input
text: The text to reverse.
Output
Return the reversed text.
Contraintes
0 ≤ length ≤ 10,000
Exemples
Entrée :
text = "hello"
Sortie attendue :
expected = "olleh"
Entrée :
std::string text = "hello";
Sortie attendue :
std::string expected = "olleh";
Entrée :
string text = "hello";
Sortie attendue :
string expected = "olleh";
Explication
h-e-l-l-o read backwards is "olleh".
Entrée :
text = "a"
Sortie attendue :
expected = "a"
Entrée :
std::string text = "a";
Sortie attendue :
std::string expected = "a";
Entrée :
string text = "a";
Sortie attendue :
string expected = "a";
Explication
A single character reversed is itself.
Comment l'aborder
Essayez d'abord seul, puis ouvrez les indices un par un.
Indice 1
The answer is the characters in opposite order.
Indice 2
A two-pointer approach or a backward traversal both work well.
Indice 3
Be sure the empty string stays empty after reversal.
Complexité attendue
O(n) time, O(n) extra space
Références
Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.