簡単 0件解決済み 正答率0%

Reverse a String

Return the characters of a string in reverse order.

このチャレンジで学べること

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

問題

翻訳を表示

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

例

例 1
入力:
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".

例 2
入力:
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 つずつ開いてみましょう。

ヒント 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用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。