# LeetCode 筆記 :(58) Length of Last Word ###### tags: `Leetcode` ```python = class Solution: def lengthOfLastWord(self, s: str) -> int: spilt_list = s.split(" ") spilt_list = list(filter(None,spilt_list)) if not spilt_list: return 0 result = len(spilt_list[-1]) return result ``` * **filter(function, iterable)** function -- 判断函数。 iterable -- 可迭代对象。 網路解法 ```python=3 class Solution: def lengthOfLastWord(self, s: str) -> int: return len(s.rstrip().split(' ')[-1]) ``` * rstrip() 可以將末段的空格刪除
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up