题目描述
![](https://i-blog.csdnimg.cn/direct/d98e3ccd61b54de1be4dacf4f3184e41.png)
思路
下意识想到先以空格作为分割符对字符串进行分割得到若干个子字符串,然后用字符串长度计算函数计算最后一个子字符串的长度。
该思路代码如下:
class Solution:
def lengthOfLastWord(self, s: str) -> int:
s_array = s.split()
last_length = len(s_array[-1])
return last_length
split():不提供参数时,默认使用空白字符作为分隔符,这意味着它会在任何连续的空白字符处分割字符串。