본문 바로가기

Leetcode

Leetcode 151. Reverse Words in a String

오늘 힘차게 시작!

 

기죽지 말고 열심히만 하자!

아자아자 파이팅

 

오늘의 문제는 Reverse Words in a String이다.

 

Input: s = "the sky is blue" Output: "blue is sky the"

인 문제이다. 

 

 

# 151. Reverse Words in a String
class Solution:
    def reverseWords(self, s: str) -> str:
        s = s.split()
        for idx, word in enumerate(s[::-1]):
            s[idx] = word + ' '
        s = ''.join(s)    
        return s[:-1]

파이썬의 여러 method 덕분에 쉽게 (?) 뚝딱 했다!

 

처음에는 공백 기준으로 split하면 되지 않을까 싶어 s.split(' ')로 delimiter을 넣어주었지만,

delimiter를 넣어주지 않고 해야 앞 뒤 공백을 없애버릴 수 있어 수정하였다.

 

가볍게 해결하였다!

 

오늘은 저 Runtime과 Memory Usage 숫자가 굉장히 마음에 든다. 히히

 

오늘도, 앞으로도 파이팅이야

 

 

'Leetcode' 카테고리의 다른 글

Leetcode 155. Min Stack  (0) 2021.10.25
Leetcode 380. Insert Delete GetRandom O(1)  (0) 2021.10.21
Leetcode 496. Next Greater Element I  (0) 2021.10.19
Leetcode 993. Cousins in Binary Tree  (0) 2021.10.18
Leetcode 733. Flood Fill  (0) 2021.10.15