[Leetcode]Reverse String

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

Example:
Given s = “hello”, return “olleh”.
我最开始是这么写的

class Solution(object):
    def reverseString(self, s):
        """
        :type s: str
        :rtype: str
        """
        a=list(s)
        a.reverse()
        m=""
        for i in a:
            m=m+i
        return m

然后测试没问题,但是提交他就跟我说时间超时
他的答案是

class Solution(object):
    def reverseString(self, s):
        """
        :type s: str
        :rtype: str
        """
        return s[::-1]          

唉,也不知道我的为什么不行,性能不行?反正记住吧

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章