Skip to content

Commit 2d6fdc8

Browse files
Merge pull request #2791 from gazeldx/l541
0541.反转字符串II.md 增加Python版本实现3
2 parents d22464f + 27585bd commit 2d6fdc8

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

problems/0541.反转字符串II.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ class Solution:
282282
return ''.join(res)
283283
```
284284

285-
### Python3 (v2):
285+
#### Python3 (v2):
286286

287287
```python
288288
class Solution:
@@ -297,6 +297,21 @@ class Solution:
297297
return s
298298
```
299299

300+
#### Python3 (v3):
301+
302+
```python
303+
class Solution:
304+
def reverseStr(self, s: str, k: int) -> str:
305+
i = 0
306+
chars = list(s)
307+
308+
while i < len(chars):
309+
chars[i:i + k] = chars[i:i + k][::-1] # 反转后,更改原值为反转后值
310+
i += k * 2
311+
312+
return ''.join(chars)
313+
```
314+
300315
### Go:
301316

302317
```go

0 commit comments

Comments
 (0)