본문 바로가기

알고리즘/파이썬 알고리즘 인터뷰

[LeetCode] 344. Reverse String (문자열 조작)

문제

https://leetcode.com/problems/reverse-string/

 

Reverse String - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

문자열을 뒤집는 함수를 만들어보자. 

입력은 문자로 구성된 배열이 주어진다. ex) ['h', 'e', 'l', 'l', 'o']

입력 배열을 in-place로 거꾸로 뒤집어야 한다. no return

 

내 풀이

class Solution:
    def reverseString(self, s: List[str]) -> None:
        s.reverse()

reverse()는 in-place로 문자열을 뒤집는 함수이다. 리스트에서만 사용할 수 있다.

문자열을 뒤집을 수 있는 또다른 방법인 슬라이싱 s = s [::-1]은 리트코드에서 사용이 안된다.