Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9e9cbb7

Browse files
🐱(string): 344. 反转字符串
补充递归解法
1 parent 106ed64 commit 9e9cbb7

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

‎docs/data-structure/string/README.md‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ for key, value in c_dict.items():
11401140

11411141
[原题链接](https://leetcode-cn.com/problems/reverse-string/)
11421142

1143-
### 思路
1143+
### 解一:双指针
11441144

11451145
头尾双指针,不停交换并往中间靠拢。
11461146

@@ -1162,6 +1162,48 @@ class Solution(object):
11621162
j = j - 1
11631163
```
11641164

1165+
### 解二:递归
1166+
1167+
<!-- tabs:start -->
1168+
1169+
#### **Python**
1170+
1171+
```python
1172+
class Solution:
1173+
def reverseString(self, s: List[str]) -> None:
1174+
"""
1175+
Do not return anything, modify s in-place instead.
1176+
"""
1177+
def helper(left, right):
1178+
if left >= right:
1179+
return
1180+
s[left], s[right] = s[right], s[left]
1181+
helper(left + 1, right - 1)
1182+
1183+
helper(0, len(s) - 1)
1184+
```
1185+
1186+
#### **Go**
1187+
1188+
```go
1189+
func reverseString(s []byte) {
1190+
helper(0, len(s) - 1, s)
1191+
}
1192+
1193+
func helper(left int, right int, s []byte) {
1194+
if left >= right {
1195+
return
1196+
}
1197+
1198+
tmp := s[left]
1199+
s[left] = s[right]
1200+
s[right] = tmp
1201+
helper(left + 1, right - 1, s)
1202+
}
1203+
```
1204+
1205+
<!-- tabs:end -->
1206+
11651207

11661208
## 345. 反转字符串中的元音字母
11671209

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /