【Python300】003-旋转字符串

作者 by Tianzhi Jia / 2022-04-11 / 暂无评论 / 206 个足迹

class Solution:
    #参数s:字符列表
    #参数offset:整数
    #返回值:无
    def rotateString(self, s, offset):
        if len(s) > 0:
            offset = offset % len(s)
        temp = (s + s)[len(s) - offset : 2 * len(s) - offset]
        for i in range(len(temp)):
            s[i] = temp[i]
#主函数
if __name__ == '__main__':
    s = ["a","b","c","d","e","f","g"]
    offset = 3
    solution = Solution()
    solution.rotateString(s, offset)
    print("输入:s =", ["a","b","c","d","e","f","g"], " ", "offset =",offset)
    print("输出:s =", s)

独特见解