问题描述
给定一个包含单词和空格的字符串,返回最后一个单词的长度。
示例
Input: s = “Hello World”
Output: 5
Explanation: The last word is “World” with length 5.
Input: s = “ fly me to the moon “
Output: 4
Explanation: The last word is “moon” with length 4.
Input: s = “luffy is still joyboy”
Output: 6
Explanation: The last word is “joyboy” with length 6.
解决方案描述
・判断字符串是否为空,如果为空,则返回0。
・去掉字符串首尾的空格,然后使用空格将其分割成字符串数组,
・判断字符串数组的长度,如果为0,则返回0;否则返回该字符串数组的长度-1的下标对应的字符串的长度。

174

被折叠的 条评论
为什么被折叠?



