python 2.7字符串反转

Lin*_* Ma 0 python python-2.7

如果我需要在Python 2.7中实现字符串反向而不是使用系统库,那么想知道是否有更高效的解决方案?我试过我的代码运行速度很慢很长的字符串(例如几千个字符).谢谢.

对于字符串反转,我的意思是,例如,给定s ="hello",返回"olleh".

def reverseString(self, s):
    """
    :type s: str
    :rtype: str
    """
    if not s:
        return ''
    temp = []
    result=''
    for i in range(len(s)-1,-1,-1):
        result += s[i]
    return result
Run Code Online (Sandbox Code Playgroud)

问候,林

Ian*_*ney 6

试试这个:

def reverseString(self, s):
    return s[::-1]
Run Code Online (Sandbox Code Playgroud)