第十周_Leetcode

题目:

344Reverse 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

运行结果:



猜你喜欢

转载自blog.csdn.net/cchsblog/article/details/80379779