From 0738423c9ed3979fa649ba7a147c4c60ecdec616 Mon Sep 17 00:00:00 2001 From: Camille0512 Date: Sun, 27 Feb 2022 00:24:13 +0800 Subject: [PATCH] modified 541. Can be more pythonic. --- problems/0541.反转字符串II.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md index 14b8601a..cd9bb0cc 100644 --- a/problems/0541.反转字符串II.md +++ b/problems/0541.反转字符串II.md @@ -204,8 +204,23 @@ class Solution: return ''.join(res) ``` +Python3 (v2): + +```python +class Solution: + def reverseStr(self, s: str, k: int) -> str: + # Two pointers. Another is inside the loop. + p = 0 + while p < len(s): + p2 = p + k + # Written in this could be more pythonic. + s = s[:p] + s[p: p2][::-1] + s[p2:] + p = p + 2 * k + return s +``` Go: + ```go func reverseStr(s string, k int) string { ss := []byte(s)