0. 문제
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
s단어와 공백으로 구성된 문자열이 주어지면 문자열 의 마지막 단어 길이를 반환
#1
Input: s = "Hello World"
Output: 5
#2
Input: s = " fly me to the moon "
Output: 4
#3
Input: s = "luffy is still joyboy"
Output: 6
1.언어
자바스크립트(JavaScript)
2. 문제 풀이
var lengthOfLastWord = function(s) {
let res=s.split(' ')
for (let i=res.length-1; i >= 0; i-- )
if (res[i].length>0) return res[i].length
};
Runtime: 74 ms, faster than 72.32% of JavaScript online submissions for Length of Last Word.
Memory Usage: 42.3 MB, less than 31.42% of JavaScript online submissions for Length of Last Word.
문자열을 공백 기준으로 배열로 치환 -> 반복문으로 인덱스 맨 뒤 부터 돌며 단어 발견 시 길이 출력
'LeetCode > 코딩 테스트 스터디 2주차' 카테고리의 다른 글
[LeetCode] 8. String to Integer (atoi) 풀이 (JS) (0) | 2022.07.17 |
---|---|
[LeetCode] 66. Plus One 풀이 (JS) (0) | 2022.07.17 |
[LeetCode] 7. Reverse Integer 풀이 (JS) (0) | 2022.07.17 |
[LeetCode] 35. Search Insert Position 풀이 (JS) (0) | 2022.07.17 |
[LeetCode] 28. Implement strStr() 풀이 (JS) (0) | 2022.07.17 |