添加 剑指Offer58-II.左旋转字符串 Python3版本 使用模+下标

考虑不能用切片的情况下,利用模+下标实现
This commit is contained in:
shuwen
2021-08-14 17:38:28 +08:00
committed by GitHub
parent 4028047d9d
commit 0c4a4a68ce

View File

@ -141,6 +141,18 @@ class Solution:
# 空间复杂度O(n)python的string为不可变需要开辟同样大小的list空间来修改 # 空间复杂度O(n)python的string为不可变需要开辟同样大小的list空间来修改
``` ```
```python 3
#方法三:考虑不能用切片的情况下,利用模+下标实现
class Solution:
def reverseLeftWords(self, s: str, n: int) -> str:
new_s = ''
for i in range(len(s)):
j = (i+n)%len(s)
new_s = new_s + s[j]
return new_s
```
Go Go
```go ```go