[LeetCode] 344. Reverse String

2021. 1. 4. 18:17Problem Solving/LeetCode

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

 

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

내 풀이

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

        i = len(s) // 2

        while i > 0:
            s[i-1], s[len(s)-i] = s[len(s)-i], s[i-1]
            i -= 1


"리턴 없이 리스트 내부를 직접 조작하라"는 조건이 있어 스왑을 해 주었다. 다른 언어와는 달리 별도의 temporary 변수를 주지 않아도 바로 리스트를 스왑 할 수 있다는걸 알게되었다...!

 

 

책 정보

파이썬 알고리즘 인터뷰
국내도서
저자 : 박상길
출판 : 책만 2020.07.15
상세보기
반응형