> For the complete documentation index, see [llms.txt](https://protegejj.gitbook.io/oj-practices/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://protegejj.gitbook.io/oj-practices/chapter1/two-pointers/344-reverse-string.md).

# 344     Reverse String

## 344. [Reverse String](https://leetcode.com/problems/reverse-string/description/)

## 1. Question

Write a function that takes a string as input and returns the string reversed.

**Example:**\
Given s = "hello", return "olleh".

## 2. Implementation

**(1) Two Pointers**

```java
class Solution {
    public String reverseString(String s) {
        int start = 0, end = s.length() - 1;
        char[] letters = s.toCharArray();

        while (start < end) {
            char temp = letters[start];
            letters[start] = letters[end];
            letters[end] = temp;
            ++start;
            --end;
        }
        return new String(letters);
    }
}
```

## 3. Time & Space Complexity

**Two Pointers:** 时间复杂度O(n), 空间复杂度O(1)
