链接: 原文链接.
class Solution: # 切片法 # def reverseLeftWords(self, s: str, n: int) -> str: # # s: # temp = s[:n] # s = s[n:] # s = s + temp # return s # def reverseLeftWords(self, s: str, n: int) -> str: # return s[n:]+s[:n] # 列表遍历拼接 # def reverseLeftWords(self, s: str, n: int) -> str: # temp = [] # for i in range(n, len(s)): # temp.append(s[i]) # for i in range(0, n): # temp.append(s[i]) # # string.join(seq):以string作为分隔符,将seq中所有的字符以string分隔开 # # 换一种思路,如果string为空,那么就是将seq中所有的字符连接起来。 # # seq可以是列表 # return ''.join(temp) # 字符串遍历拼接 def reverseLeftWords(self, s: str, n: int) -> str: temp = '' for i in range(n, len(s)): temp += s[i] for i in range(0, n): temp += s[i] return temp # 效率比较 切片>列表遍历拼接>字符串遍历拼接 # os:虽然字符串遍历拼接是最熟悉的但是效率却是最低下的。。