Skip to content

344 reverse string

344. Reverse String

题目: https://leetcode.com/problems/reverse-string/

难度: Easy

思路:

不要脸的python AC code:

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

因为python不支持item assignment

所以如果非要用two pointer来做的话,那么会是这样

class Solution(object):
    def reverseString(self, s):
        """
        :type s: str
        :rtype: str
        """
        lst = list(s)
        n = len(lst)
        start, end = 0, n - 1

        while start < end:
            lst[end], lst[start]  = lst[start],lst[end]
            start += 1
            end -= 1
        return ''.join(lst)


回到顶部