Leetcode 344:验证回文串(最详细解决方案!!!)

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

示例:

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

解题思路

这个问题没什么好说的,有一个pythonic式的解法

class Solution:
    def reverseString(self, s):
        """
        :type s: str
        :rtype: str
        """
        return s[::-1]

但是我们这里同样可以参考Leetcode 167:两数之和 II - 输入有序数组中使用的对撞指针的思路。

class Solution:
    def reverseString(self, s):
        """
        :type s: str
        :rtype: str
        """
        l = 0
        r = len(s) - 1
        s = list(s)
        while l < r:
            s[l], s[r] = s[r], s[l]
            l += 1
            r -= 1

        return ''.join(s)

但是这个解法在python看来很蠢( ̄▽ ̄)”~~

该问题的其他语言版本添加到了我的GitHub Leetcode

如有问题,希望大家指出!!!

猜你喜欢

转载自blog.csdn.net/qq_17550379/article/details/80513863