[LeetCode] 344.Reverse String

This question is very simple, is the basic version, the time complexity of the double pointer problem is O (N), the spatial complexity is O (1)

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

Guess you like

Origin www.cnblogs.com/codingEskimo/p/12220197.html