개발공부/algorithm
[Leetcode][python] 58: Length of Last Word
so.py
2021. 6. 24. 20:13
문제
- Leetcode 58: Length of Last word
- Level: Easy
- Link
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
나의 풀이
쉬워보이지만 반례가 많아서 세심한 디버깅이 필요한 문제다.
받은 스트링 값을 " "로 split해주고, 뒤에서부터 탐색하며 길이가 0이 아닌 element를 리턴해준다.
class Solution:
def lengthOfLastWord(self, s: str) -> int:
lst = s.split(' ')
for i in range(len(lst) - 1 , -1, -1):
if len(lst[i]) != 0:
return (len(lst[i]))
return 0