此篇文章為我的解題紀錄,程式碼或許並不是很完善

Leetcode - 58. Length of Last Word

解題思路

將字串用空白切割後再將空字串過濾掉,就可以輸出最後一個字的長度囉~

我滴程式碼

1
2
3
4
5
class Solution:
def lengthOfLastWord(self, s: str) -> int:
word = s.split(' ')
word = list(filter(None, word))
return len(word[len(word) - 1])