Algorithm/LeetCode

[LeetCode] 557. Reverse Words in a String III

rrojin 2022. 3. 3. 11:26

문제링크

https://leetcode.com/problems/reverse-words-in-a-string-iii/

 

Reverse Words in a String III - 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

 

코드

1
2
3
4
5
6
7
8
9
class Solution:
    def reverseWords(self, s: str-> str:
        words = s.split()
        ans = ""
        for i,word in enumerate(words):
            ans+=  word[::-1]
            if i != len(words)-1:
                ans+=" "
        return ans
cs

 

POINT

문장을 " "기준으로 나누기 위해 split()을 사용하였다.

이후 매 단어마다 [::-1] 파이썬 방식의 reverse를 사용하여 단어를 반전시켰다.

 

 

 

 

 

 

 

'Algorithm > LeetCode' 카테고리의 다른 글

[LeetCode] 344. Reverse String  (0) 2022.03.03
[LeetCode] 167. Two Sum II - Input Array Is Sorted  (0) 2022.03.02
[LeetCode] 283. Move Zeroes  (0) 2022.03.02
[LeetCode] 189. Rotate Array  (0) 2022.03.01
[LeetCode] 977. Squares of a Sorted Array  (0) 2022.03.01