알고리즘/leetcode
[LeetCode][JavaScript] 58. Length of Last Word
CooNiHong
2021. 7. 28. 16:11
[LeetCode][JavaScript] 58. Length of Last Word
문제 출처: https://leetcode.com/problems/length-of-last-word/
Length of Last Word - 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
[직접 푼 코드]
var lengthOfLastWord = function(s) {
return s.trim().split(' ').reverse()[0].length;
};
[문제 풀이]
1. 혹시모를 문자열 앞뒤에 공백을 제거 (trim 메서드)
2. 띄어쓰기 기준으로 문자열을 배열로 만듭니다. (split 메서드)
3. 마지막 단어가 배열의 맨 앞으로 오게 만든 후 문자열의 길이를 구합니다. (reverse 메서드)
알고리즘 스터디: https://github.com/ROUTINE-STUDY/Algorithm
GitHub - ROUTINE-STUDY/Algorithm: 초보 알고리즘 스터디 / 누구나 참여 가능
초보 알고리즘 스터디 / 누구나 참여 가능 :runner:. Contribute to ROUTINE-STUDY/Algorithm development by creating an account on GitHub.
github.com
누구나 참여 가능한 알고리즘 스터디입니다.
알고리즘뿐만 아니라 개발을 하면서 겪은 이슈, 이론을 정리하는 Routine-Study를 운영 중입니다.
728x90