|
| 1 | +# [344. 反转字符串](https://leetcode-cn.com/problems/reverse-string/description/) |
| 2 | + |
| 3 | + |
| 4 | +### 题目描述 |
| 5 | + |
| 6 | +<p>编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 <code>char[]</code> 的形式给出。</p> |
| 7 | + |
| 8 | +<p>不要给另外的数组分配额外的空间,你必须<strong><a href="https://baike.baidu.com/item/原地算法" target="_blank">原地</a>修改输入数组</strong>、使用 O(1) 的额外空间解决这一问题。</p> |
| 9 | + |
| 10 | +<p>你可以假设数组中的所有字符都是 <a href="https://baike.baidu.com/item/ASCII" target="_blank">ASCII</a> 码表中的可打印字符。</p> |
| 11 | + |
| 12 | +<p> </p> |
| 13 | + |
| 14 | +<p><strong>示例 1:</strong></p> |
| 15 | + |
| 16 | +<pre><strong>输入:</strong>["h","e","l","l","o"] |
| 17 | +<strong>输出:</strong>["o","l","l","e","h"] |
| 18 | +</pre> |
| 19 | + |
| 20 | +<p><strong>示例 2:</strong></p> |
| 21 | + |
| 22 | +<pre><strong>输入:</strong>["H","a","n","n","a","h"] |
| 23 | +<strong>输出:</strong>["h","a","n","n","a","H"]</pre> |
| 24 | + |
| 25 | +### 解题思路 |
| 26 | + |
| 27 | +1. 双指针操作 |
| 28 | + |
| 29 | +### 代码实现 |
| 30 | + |
| 31 | +<!-- tabs:start --> |
| 32 | + |
| 33 | +#### **Golang** |
| 34 | +```go |
| 35 | +func reverseString(s []byte) { |
| 36 | + p, q := 0, len(s)-1 |
| 37 | + for p < q { |
| 38 | + s[p], s[q] = s[q], s[p] |
| 39 | + p++ |
| 40 | + q-- |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | + |
| 46 | +<!-- tabs:end --> |
0 commit comments