-
[알고리즘] #Reverse Words in a String III■ Algorithm 2022. 8. 5. 17:37
문제 출처: 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
문제 해석
문자 s가 주어졌을 때, 단어 순서나 공백은 유지한채 단어를 역순으로 리턴하는 함수를 구현하시오.
풀이
이전에 string을 역순으로 구하는 문제를 reverse()가 아닌 다른 방법으로 풀었으니, 이번에는 reverse()를 사용해서 풀어보았다.
공백으로 문자를 나누고, 역순을 한 다음에 다시 join 해줬다.
var reverseWords = function(s) { const words = s.split(" ").map((word) => { return word.split('').reverse().join('') }) return words.join(' ') };
'■ Algorithm' 카테고리의 다른 글
[알고리즘] #Remove Nth Node From End of List (0) 2022.08.05 [알고리즘] #Middle of the Linked List (0) 2022.08.05 [알고리즘] #Reverse String (0) 2022.08.05 [알고리즘] #Two Sum II - Input Array Is Sorted (0) 2022.08.05 [알고리즘] #Move Zeroes (0) 2022.08.03