> For the complete documentation index, see [llms.txt](https://wenkangwei.gitbook.io/leetcode-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wenkangwei.gitbook.io/leetcode-notes/datastructure/string-1/reverse-string.md).

# Reverse String

### 1. Link

{% embed url="<https://www.nowcoder.com/practice/c3a6afee325e472386a1c4eb1ef987f3?tpId=188&tqId=38280&rp=1&ru=%2Factivity%2Foj&qru=%2Fta%2Fjob-code-high-week%2Fquestion-ranking&tab=answerKey>" %}

{% embed url="<https://leetcode.cn/problems/reverse-words-in-a-string/>" %}

### 2. 题目描述

写出一个程序，接受一个字符串，然后输出该字符串反转后的字符串。（字符串长度不超过1000）示例1

### 输入

复制

```
"abcd"
```

### 返回值

复制

```
"dcba"
```

### 3. 思路

1. &#x20;因为String在python是immutable 不可变，每次操作都增加新字符串，所以要先把它变成list
2. 在list中，用two pointers/左右pointers 方法从左右两边进行char的交换，并把 两个pointer向中间移动
3. 重复步骤2 直到 left >= right 时停止
4. 返回reverse之后的字符串
5. Time Complexity: O(n)  Space Complexity: O(n) 因为创建了一个list和返回新的字符串

### 4. Coding

```
#
# 反转字符串
# @param str string字符串 
# @return string字符串
#
class Solution:
    def solve(self , str ):
        # write code here
        ls = list(str)
        left, right = 0, len(ls)-1
        while left < right:
            ls[left], ls[right] = ls[right], ls[left]
            left += 1
            right -= 1
        return "".join(ls)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wenkangwei.gitbook.io/leetcode-notes/datastructure/string-1/reverse-string.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
