LeetCode:Length of Last Word
Posted walker lee
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode:Length of Last Word相关的知识,希望对你有一定的参考价值。
Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ‘
‘
, return the length of last word in the string.
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
For example,
Given s = "Hello World"
,
return 5
.
Subscribe to see which companies asked this question
c++ code:
class Solution { public: int lengthOfLastWord(string s) { int tail = s.size() - 1; int cnt=0; while(tail>=0 && ' ' == s[tail]) tail--; while(tail>=0 && s[tail]!=' ') { cnt++; tail--; } return cnt; } };
以上是关于LeetCode:Length of Last Word的主要内容,如果未能解决你的问题,请参考以下文章
[LeetCode] Length of Last Word
[leetcode] length-of-last-word
[Leetcode] Length of last word 最后一个单词的长度
leetcode Length of Last Word(easy) /java
leetcode : Length of Last Word [基本功]
nth-child,nth-last-child,only-child,nth-of-type,nth-last-of-type,only-of-type,first-of-type,last-of-