[LeetCode] Length of Last Word

동스토리 ㅣ 2021. 8. 31. 04:38

반응형

안녕하세요.

 

LeetCode 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

 

[문제]

 

[TestCase]

 

 

[해설]

 

- 리스트를 공백으로 자른다

- 끝자리부터 0번째 까지 내려오면서 마지막 문자열의 길이가 0이 아닐경우 출력

 

[코드]

 

class Solution:
    def lengthOfLastWord(self, s: str) -> int:
        
        result = s.split(' ')
        
        for i in range(len(result), 0, -1):
            if len(result[i-1]) !=0:
                return len(result[i-1])
            else:
                pass

 

감사합니다.

반응형

'Development > Algorithm' 카테고리의 다른 글

[LeetCode] Add Binary  (0) 2021.08.31
[LeetCode] Plus One  (0) 2021.08.31
[LeetCode] Search Insert Position  (0) 2021.08.26
[LeetCode] Implement strStr()  (0) 2021.08.26
[LeetCode] Remove Element  (0) 2021.08.24