less than 1 minute read

344. Reverse String

문제 링크

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

문제 설명

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.

제한사항

  1. 1 <= s.length <= 105
  2. s[i] is a printable ascii character.

입출력 예 #1

  1. Input: s = [“h”,”e”,”l”,”l”,”o”]
  2. Output: [“o”,”l”,”l”,”e”,”h”]

입출력 예 #2

  1. Input: s = [“H”,”a”,”n”,”n”,”a”,”h”]
  2. Output: [“h”,”a”,”n”,”n”,”a”,”H”]

문제 풀이

리스트 뒤집기 함수로 풀이할 수 있다.

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

Comments