[Leetcode] 151 Reverse Words in a String (Python)leetcode2025. 9. 23. 22:37
목차
문제
Reverse Words in a String - LeetCode
설명
Given an input string s, reverse the order of the words.
A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.
Return a string of the words in reverse order concatenated by a single space.
Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
풀이 계획
s를 공백으로 나누고, 뒤집어준 결과물을 돌면서 빈 리스트에 하나씩 담는다. 채워진 리스트를 공백으로 합쳐 하나의 문자열로 반환한다.
초기 풀이 (0ms, 18.03MB)
class Solution:
def reverseWords(self, s: str) -> str:
res = []
for i in reversed(s.split()):
res.append(i)
return ' '.join(res)
시간복잡도 : O(N)
공간복잡도 : O(N)
728x90
'leetcode' 카테고리의 다른 글
| [Leetcode] 724 Find Pivot Index (Python) (0) | 2025.09.29 |
|---|---|
| [Leetcode] 2390 Removing Starts From a String (Python) (0) | 2025.09.24 |
| [Leetcode] 1732 Find the Highest Altitude (Python) (0) | 2025.09.22 |
| [Leetcode] 1456 Maximum Number of Vowels in a Substring of Given Length (Python) (0) | 2025.09.17 |
| [Leetcode] 392 Is Subsequence (Python) (0) | 2025.09.16 |
@kdj :: Childev'note
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!