728x90
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
i, j = 0, len(s) - 1
while i < j:
s[i], s[j] = s[j], s[i]
i += 1
j -= 1
왼쪽 끝부터, 오른쪽 끝부터
탐색하면서 서로 swap
string의 길이가 짝, 홀 상관없이 i<j 조건에서는 해결됨.
다른 풀이
s가 리스트이기 때문에 list.reverse()를 사용하면 됨.
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
s.reverse()728x90
'Leetcode' 카테고리의 다른 글
| [LeetCode] Intersection of Two Arrays 파이썬 (set, intersection) (0) | 2021.04.13 |
|---|---|
| [LeetCode] Reverse Vowels of String 파이썬 (swap, two pointer) (0) | 2021.04.13 |
| [LeetCode] Word Pattern 파이썬 (isomorphic, contract mapping) (0) | 2021.04.12 |
| [LeetCode] Move Zeros 파이썬 (in-place) (0) | 2021.04.12 |
| [LeetCode] Missing Number 파이썬 (0) | 2021.04.12 |
댓글