modified 541. Can be more pythonic.

This commit is contained in:
Camille0512
2022-02-27 00:24:13 +08:00
parent 4980496c05
commit 0738423c9e

View File

@ -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)