您的位置:首页 > 其它

344. Reverse String

2017-05-09 21:58 309 查看

344. Reverse String

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

Example:
Given s = "hello", return "olleh".


解:

可以直接用python的字符串处理,也可以用交换字符的位置来完成,交换位置的话需要先把字符串拆分成一个个字符,因为字符串是不可变的,最后用了join()来拼接字符。

class Solution(object):
def reverseString(self, s):
"""
:type s: str
:rtype: str
"""
#return s[::-1]
s = list(s)
i = 0
j = len(s) - 1
while(i < j):
temp = s[i]
s[i] = s[j]
s[j] = temp
i += 1
j -= 1
return ''.join(s)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: