Tutte le sfide
Facile 1 risolta 100.0% di accettazione

Reverse a String

Return the characters of a string in reverse order.

Cosa imparerai

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

Problema

Mostra traduzione

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

Esempio 1
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".

Esempio 2
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.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

# Programmatore Linguaggio Tempo Memoria Voto dell'IA Soluzione
1 KKKaneki Ken Python 773 ms - 5/5 Vedi il codice

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 1 soluzione completa sotto Prova prima a risolverla da solo. Puoi comunque sbirciare il codice degli altri sviluppatori e le recensioni AI. Accedi e risolvi la sfida per partecipare alla discussione.