LeetCode58

LeetCode58:https://leetcode-cn.com/problems/length-of-last-word/

解题思路:利用字符串的内置函数

1 class Solution:
2     def lengthOfLastWord(self, s: str) -> int:
3         return len(s.strip().split(' ')[-1])

字符串:

strip()方法:移除字符串头尾指定的字符(默认为空格)或字符序列。

split() 通过指定分隔符对字符串进行切片,返回分割后的字符串列表。

 

posted @ 2021-02-19 11:53  vv_869  阅读(85)  评论(0编辑  收藏  举报