题目
代码
执行用时:36 ms, 在所有 Python3 提交中击败了61.20% 的用户
内存消耗:15 MB, 在所有 Python3 提交中击败了78.80% 的用户
通过测试用例:34 / 34
class Solution:
def reverseLeftWords(self, s: str, n: int) -> str:
return s[n:]+s[:n]
【方法2】
执行用时:36 ms, 在所有 Python3 提交中击败了61.20% 的用户
内存消耗:15.1 MB, 在所有 Python3 提交中击败了38.62% 的用户
通过测试用例:34 / 34
class Solution:
def reverseLeftWords(self, s: str, n: int) -> str:
ans=[' ']*len(s)
for i in range(len(s)):
if i>=n:
ans[i-n]=s[i]
else:
ans[len(s)-n+i]=s[i]
return "".join(ans)
【方法3】
执行用时:36 ms, 在所有 Python3 提交中击败了61.20% 的用户
内存消耗:15.1 MB, 在所有 Python3 提交中击败了37.15% 的用户
通过测试用例:34 / 34
class Solution:
def reverseLeftWords(self, s: str, n: int) -> str:
ans=[]
for i in range(n,n+len(s)):
ans.append(s[i%len(s)])
return "".join(ans)