From a4cafe0ce750c44f327a97a9811ff0b2d2c49979 Mon Sep 17 00:00:00 2001 From: Lane Zhang Date: Wed, 16 Oct 2024 11:25:49 +0800 Subject: [PATCH 1/2] =?UTF-8?q?0541.=E5=8F=8D=E8=BD=AC=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2II.md=20=E5=A2=9E=E5=8A=A0Python=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=AE=9E=E7=8E=B03?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0541.反转字符串II.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md index 5e75d3c3..c89b3a7b 100644 --- a/problems/0541.反转字符串II.md +++ b/problems/0541.反转字符串II.md @@ -282,7 +282,7 @@ class Solution: return ''.join(res) ``` -### Python3 (v2): +#### Python3 (v2): ```python class Solution: @@ -297,6 +297,20 @@ class Solution: return s ``` +#### Python3 (v3): +```python +class Solution: + def reverseStr(self, s: str, k: int) -> str: + i = 0 + chars = list(s) + + while i < len(chars): + chars[i:i + k] = chars[i:i + k][::-1] # 反转后,更改原值为反转后值 + i += k * 2 + + return ''.join(chars) +``` + ### Go: ```go From 27585bd4f8fc7b4f0599f231d4924641e1bf6deb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lane=20Zhang=20=28=E5=BC=A0=E5=81=A5=29?= Date: Wed, 16 Oct 2024 11:27:38 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=200541.=E5=8F=8D=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0541.反转字符串II.md | 1 + 1 file changed, 1 insertion(+) diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md index c89b3a7b..b3e7b022 100644 --- a/problems/0541.反转字符串II.md +++ b/problems/0541.反转字符串II.md @@ -298,6 +298,7 @@ class Solution: ``` #### Python3 (v3): + ```python class Solution: def reverseStr(self, s: str, k: int) -> str: