leetcode-反转字符串

344. 反转字符串

此题目已经说明,只能原地修改输入的数组,不能有额外的空间占用。使用双指针,从数组的两端开始,两两交换位置,达到了反转的作用。

class Solution:
    def reverseString(self, s: List[str]) -> None:
        """
        Do not return anything, modify s in-place instead.
        """
        left = 0
        right = len(s) - 1
        while left < right:
            s[left], s[right] = s[right], s[left]
            left += 1
            right -= 1
        return s

你可能感兴趣的:(leetcode)