文章目录
- Write a function that reverses a string. The input string is given as an array of characters s. You must do this by modifying the input array in-place with O(1) extra memory.
- Two Pointer
- class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ # s.reverse() # The List Function is the easiest way slow = 0 while(slow < len(s) - slow - 1): tmp = s[slow] s[slow] = s[len(s) - slow -1] s[len(s) - slow - 1] = tmp slow += 1
Write a function that reverses a string. The input string is given as an array of characters s.
You must do this by modifying the input array in-place with O(1) extra memory.
Two Pointer
Two Pointer
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
# s.reverse()
# The List Function is the easiest way
slow = 0
while(slow < len(s) - slow - 1):
tmp = s[slow]
s[slow] = s[len(s) - slow -1]
s[len(s) - slow - 1] = tmp
slow += 1
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
# s.reverse()
# The List Function is the easiest way
slow = 0
while(slow < len(s) - slow - 1):
tmp = s[slow]
s[slow] = s[len(s) - slow -1]
s[len(s) - slow - 1] = tmp
slow += 1
