https://leetcode.com/problems/reverse-string/
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
s.reverse()
reverse() 함수 사용
reverse
리스트 => reverse()
문자열 => 슬라이싱 이용s[::-1]/s[:] = s[::-1]이 문제는
O(1)extra memory 이므로s[:] = s[::-1]사용
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
for i in range(len(s)//2):
s[i], s[len(s)-i-1] = s[len(s)-i-1], s[i]
좌우대칭으로 교환
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
left, right = 0, len(s)-1
while left < right:
s[left], s[right] = s[right], s[left]
left += 1
right -= 1
Two-Pointer