문제
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
Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
문자열 s가 주어지면 공백과 초기 단어 순서를 유지하면서 문장 내 각 단어의 문자 순서를 반대로 뒤집는다.
예제 1
Input | Output |
s = "Let's take LeetCode contest" | "s'teL ekat edoCteeL tsetnoc" |
문제풀이 코드
class Solution:
def reverseWords(self, s: str) -> str:
return ' '.join([''.join(reversed(list(x))) for x in s.split(' ')])
1. 문자열 s를 띄어쓰기로 split한다
2. x를 리스트로 변환하고 reversed한다.
3. 뒤집어진 리스트를 다시 문자열로 바꾸고 각 단어를 띄어쓰기로 합쳐준다.
'Problem Solving > Leetcode' 카테고리의 다른 글
[Leetcode] 11. Container With Most Water (4) | 2022.09.25 |
---|---|
[Leetcode] 1680. Concatenation of Consecutive Binary Numbers (0) | 2022.09.24 |
[Leetcode] 3. Longest Substring Without Repeating Characters (0) | 2022.09.22 |
[Leetcode] 2. Add Two Numbers (0) | 2022.09.18 |
[Leetcode] 26. Remove Duplicates from Sorted Array (0) | 2022.09.17 |