LeetCode:Length of Last Word
Posted walker lee
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode:Length of Last Word相关的知识,希望对你有一定的参考价值。
Length of Last Word
Total Accepted: 94220 Total
Submissions: 319858 Difficulty: Easy
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的主要内容,如果未能解决你的问题,请参考以下文章