LintCode #8. 旋转字符串
[问题地址]:https://www.lintcode.com/problem/rotate-string/description
解法一:
class Solution:
"""
@param: str: An array of char
@param: offset: An integer
@return: nothing
"""
def rotateString(self, str, offset):
# write your code here
if len(str) > 0:
offset = offset % len(str)
M = offset
str[:-M] = str[:-M][::-1]
str[-M:] = str[-M:][::-1]
str[:] = str[:][::-1]
说明:熟练运用字符串与列表的切片功能,能极大简化编程逻辑。
注意:str[:-M][::-1]是首先对str切片再颠倒字符顺序。str[:-M:-1]是从字符串尾部开始到倒数第M个数的切片。