오늘의 문제는 String을 뒤집는 것인데, 단어들 (공백기준) 사이에는 순서를 유지하는 것이다.
class Solution:
def reverseWords(self, s: str) -> str:
answer = ""
s = s.split(' ')
for word in s:
answer += word[::-1]
answer += " "
return answer[:-1]
for문에서 마지막 word에서, answer에 공백이 하나 더 들어가게 된다.
이를 처리해주기 위해 return 문에서 [:-1]로 공백을 제외한 나머지 문자열을 return하게 한다.
사실 처음에 마지막 space에 대한 생각을 해주지 않아 Wrong Answer을 받았다...흑
그래도 금방 코드가 뚝딱뚝딱 짜져서 (?) 기분이 좋다!
벌써 금요일이다. 이번 일주일 동안 열심히 살아왔다고 자부할 수 있다. (물론 많이 자기도 했지만)
갑자기 든 생각이지만 이 세상 모두가 행복해졌으면 좋겠다!
오늘도 끝!
'Leetcode' 카테고리의 다른 글
Leetcode 3. Longest Substring Without Repeating Characters (0) | 2021.10.13 |
---|---|
Leetcode 876. Middle of the Linked List (0) | 2021.10.12 |
Leetcode 167. Two Sum II - Input array is sorted (0) | 2021.10.07 |
Leetcode 283. Move Zeroes (0) | 2021.10.06 |
Leetcode 189. Rotate Array (0) | 2021.10.05 |