class Solution:
def reverseLeftWords(self, s: str, n: int) -> str:
ls = list(s)
for i in range(n):
ls.append(ls.pop(0))
return "".join(ls)
时间复杂度:
O
(
n
)
O(n)
O(n)
空间复杂度:
O
(
1
)
O(1)
O(1)
使用取余操作
class Solution:
def reverseLeftWords(self, s: str, n: int) -> str:
# 使用取余操作:在最后将索引变回到最前
res = ""
for i in range(n, n+len(s)):
res += s[i%len(s)]
return res