mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Update 0344.反转字符串.md
This commit is contained in:
@ -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) {
|
||||
|
Reference in New Issue
Block a user