Problema
Reverse text so the last character comes first.
Implement reverse_string(text).
Input
text: The text to reverse.
Output
Return the reversed text.
Vincoli
0 ≤ length ≤ 10,000
Esempi
Input:
text = "hello"
Output atteso:
expected = "olleh"
Input:
std::string text = "hello";
Output atteso:
std::string expected = "olleh";
Input:
string text = "hello";
Output atteso:
string expected = "olleh";
Spiegazione
h-e-l-l-o read backwards is "olleh".
Input:
text = "a"
Output atteso:
expected = "a"
Input:
std::string text = "a";
Output atteso:
std::string expected = "a";
Input:
string text = "a";
Output atteso:
string expected = "a";
Spiegazione
A single character reversed is itself.
Come affrontarlo
Prova prima da solo, poi apri un suggerimento alla volta.
Suggerimento 1
The answer is the characters in opposite order.
Suggerimento 2
A two-pointer approach or a backward traversal both work well.
Suggerimento 3
Be sure the empty string stays empty after reversal.
Complessità attesa
O(n) time, O(n) extra space
Riferimenti
Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.