leetcode之Reverse String(344)

题目:

请编写一个函数,其功能是将输入的字符串反转过来。

示例:

输入:s = "hello"
返回:"olleh"

Python代码1:

class Solution:
    def reverseString(self, s):
        ss = ''
        for i in range(len(s)-1,-1,-1):
            ss += s[i]
        return ss

Python代码2:

class Solution:
    def reverseString(self, s):
        return s[::-1]

心得:这可能是我见过leetcode所有题里边最简单的一道了吧,在python里边,字符串反转常用方法2。

猜你喜欢

转载自blog.csdn.net/cuicheng01/article/details/81326055