Einfach 0 gelöst 0% Erfolgsquote

Reverse a String

Return the characters of a string in reverse order.

Das lernst du

  • Walk through a string backwards
  • Build a new string from characters
  • Use slicing or two pointers

Problem

Übersetzung anzeigen

Reverse text so the last character comes first.

Implement reverse_string(text).

Input

text: The text to reverse.

Output

Return the reversed text.

Einschränkungen

0 ≤ length ≤ 10,000

Beispiele

Beispiel 1
Eingabe:
text = "hello"

Erwartete Ausgabe:
expected = "olleh"
Eingabe:
std::string text = "hello";

Erwartete Ausgabe:
std::string expected = "olleh";
Eingabe:
string text = "hello";

Erwartete Ausgabe:
string expected = "olleh";

Erläuterung

h-e-l-l-o read backwards is "olleh".

Beispiel 2
Eingabe:
text = "a"

Erwartete Ausgabe:
expected = "a"
Eingabe:
std::string text = "a";

Erwartete Ausgabe:
std::string expected = "a";
Eingabe:
string text = "a";

Erwartete Ausgabe:
string expected = "a";

Erläuterung

A single character reversed is itself.

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

The answer is the characters in opposite order.

Hinweis 2

A two-pointer approach or a backward traversal both work well.

Hinweis 3

Be sure the empty string stays empty after reversal.

Erwartete Komplexität

O(n) time, O(n) extra space

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.