58.最后一个单词的长度

小明 2025-04-29 06:27:30 6

index.php/tags-41973.html" class="superseo">���你一个字符串 s,由若干单词组成,单词前后用一些空格字符隔开。返回字符串中 最后一个 单词的长度。

()

单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。

示例 1:

()
输入:s = "Hello World"
输出:5
解释:最后一个单词是“World”,长度为

示例 2:

输入:s = "   fly me   to   the moon  "
输出:4
解释:最后一个单词是“moon”,长度为

示例 3:

输入:s = "luffy is still joyboy"
输出:6
解释:最后一个单词是长度为6的“joyboy”。
class Solution {
public:
    int lengthOfLastWord(string s) {
int n=0;
for(int i=s.size()-1;i>=0;i--)
{
    if(s[i]!=' ')
    n++;
    if(n!=0&&s[i]==' ')
    break;
}
return n;
    }
};

从后往前搜索,遇到第一个空格就跳出,并输出此时已经有了的字符串格式。

The End
微信