补充 剑指Offer58-II.左旋转字符串.md python方法

如果不让使用自带函数reversed() 可以使用该方法
This commit is contained in:
Eyjan_Huang
2021-08-19 21:01:20 +08:00
committed by GitHub
parent 51e719a435
commit 6255eae04c

View File

@ -136,13 +136,28 @@ class Solution:
# return "".join(s)
# 方法三如果连reversed也不让使用那么自己手写一个
class Solution:
def reverseLeftWords(self, s: str, n: int) -> str:
def reverse_sub(lst, left, right):
while left < right:
lst[left], lst[right] = lst[right], lst[left]
left += 1
right -= 1
res = list(s)
end = len(res) - 1
reverse_sub(res, 0, n - 1)
reverse_sub(res, n, end)
reverse_sub(res, 0, end)
return ''.join(res)
# 时间复杂度O(n)
# 空间复杂度O(n)python的string为不可变需要开辟同样大小的list空间来修改
```
```python 3
#方法:考虑不能用切片的情况下,利用模+下标实现
#方法:考虑不能用切片的情况下,利用模+下标实现
class Solution:
def reverseLeftWords(self, s: str, n: int) -> str:
new_s = ''