344. Reverse String

Write a function that takes a string as input and returns the string reversed.

Example: Given s = “hello”, return “olleh”.

思路: 先转换成列表,然后直接通过reverse()函数来实现 关键在于字符串与列表之间的相互转换

class Solution(object):
    def reverseString(self, s):
        """
        :type s: str
        :rtype: str
        """
        lstr = []
        for c in s:
            lstr.append(c)
        lstr.reverse()
        #print(''.join(lstr))
        return ''.join(lstr)

if __name__ == "__main__":
    s = "hello"
    print(Solution().reverseString(s))

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦