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)