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]
唉,也不知道我的爲什麼不行,性能不行?反正記住吧