Account Login - LeetCode

Python Arrays

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

Recursive

Here is an example. Let's implement recursive function helper which receives two pointers, left and right, as arguments.

To solve the problem, call helper function passing the head and tail indexes as arguments: return helper(0, len(s) - 1).

Let's see this in action.

Let's say the string is "hello".

["h","e","l","l","o"]

First call:

helper(0, 4)
s[left], s[right] = s[right], s[left]

>>> x[0], x[4] = x[4], x[0]
>>> x
['o', 'e', 'l', 'l', 'h']

helper(1, 3)

Breaks when left pointer is more than right pointer.