lintcode 8.旋转字符串

class Solution:
    """
    @param str: An array of char
    @param offset: An integer
    @return: nothing
    """
    def rotateString(self, s, offset):
        # write your code here
        newstr=s[-offset:]+s[0:len(s)-offset]
        return newstr
'''
有问题
'''

第一次改动,显示超时
class Solution:
    """
    @param str: An array of char
    @param offset: An integer
    @return: nothing
    """
    def rotateString(self, s, offset):
        # write your code here
        if not offset:
            return
        if not s:
            return
        for i in range(offset):
            a=s.pop()
            s.insert(0,a)
第二次改动
class Solution:
    """
    @param str: An array of char
    @param offset: An integer
    @return: nothing
    """
    def rotateString(self, s, offset):
        # write your code here
        if not offset:
            return
        if not s:
            return
        offset=offset%len(s)
        for i in range(offset):
            a=s.pop()
            s.insert(0,a)

你可能感兴趣的:(lintcode,python)