题目:
344. Reverse String
题目描述:
Write a function that takes a string as input and returns the string reversed.
测试样例:
Given s = "hello", return "olleh".
思路分析:
string是不可变数组类型,要想改变字符串顺序需要采用s1 = s1[:i]+s[n-1-i]+s1[i+1:]形式实现。
代码:
class Solution:
def reverseString(self, s):
s1 = s
n = len(s)
i = 0
while i<n:
s1 = s1[:i]+s[n-1-i]+s1[i+1:]
i = i+1
return s1
运行结果: