[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]          

唉,也不知道我的爲什麼不行,性能不行?反正記住吧

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