美文网首页
58.Length of last word

58.Length of last word

作者: 花落花开花满天 | 来源:发表于2018-10-31 23:45 被阅读0次

Given astrings consists of upper/lower-casealphabetsandempty space characters' ',returnthe length of last word in the string.

If the last word doesnotexist,return0.

Note: A word is defined as a character sequence consists of non-space characters only.

Example:

Input: "Hello World"

Output:

5

求句末单词的长度

代码:

class Solution {

public:

    int lengthOfLastWord(string s) {

int right=s.length()-1,res=0;

    if(s.length()==0)

        return 0;

      while(right>=0 && s[right]==' ')

      {

          right--;

      }

    while (right>=0 && s[right]!=' ') {

        res++;

        right--;

    }

    return res;

    }

};

相关文章

网友评论

      本文标题:58.Length of last word

      本文链接:https://www.haomeiwen.com/subject/gwzztqtx.html