Update 0344.反转字符串.md

This commit is contained in:
jianghongcheng
2023-05-06 16:51:15 -05:00
committed by GitHub
parent b617532ce9
commit bb8b112ae0

View File

@ -174,6 +174,7 @@ class Solution {
```
Python
(版本一) 双指针
```python
class Solution:
def reverseString(self, s: List[str]) -> None:
@ -190,7 +191,62 @@ class Solution:
right -= 1
```
(版本二) 使用栈
```python
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
stack = []
for char in s:
stack.append(char)
for i in range(len(s)):
s[i] = stack.pop()
```
(版本三) 使用range
```python
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
n = len(s)
for i in range(n // 2):
s[i], s[n - i - 1] = s[n - i - 1], s[i]
```
(版本四) 使用reversed
```python
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
s[:] = reversed(s)
```
(版本五) 使用切片
```python
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
s[:] = s[::-1]
```
(版本六) 使用列表推导
```python
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
s[:] = [s[i] for i in range(len(s) - 1, -1, -1)]
```
Go
```Go
func reverseString(s []byte) {