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.

Notice

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

Have you met this question in a real interview?

 
 
Example

Given s = "Hello World", return 5.

LeetCode上的原题,请参见我之前的博客Length of Last Word

解法一:

class Solution {
public:
/**
* @param s A string
* @return the length of last word
*/
int lengthOfLastWord(string& s) {
if (s.empty()) return ;
int res = ;
if (s[] != ' ') res = ;
for (int i = ; i < s.size(); ++i) {
if (s[i] != ' ') {
if (s[i - ] == ' ') res = ;
else ++res;
}
}
return res;
}
};

解法二:

class Solution {
public:
/**
* @param s A string
* @return the length of last word
*/
int lengthOfLastWord(string& s) {
int tail = s.size() - , res = ;
while (tail >= && s[tail] == ' ') --tail;
while (tail >= && s[tail] != ' ' ) {
--tail;
++res;
}
return res;
}
};

[LintCode] Length of Last Word 求末尾单词的长度的更多相关文章

  1. [LeetCode] Length of Last Word 求末尾单词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  2. [LeetCode] 58. Length of Last Word 求末尾单词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  3. lintcode:Length of Last Word 最后一个单词的长度

    题目: 最后一个单词的长度 给定一个字符串, 包含大小写字母.空格' ',请返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 样例 给定 s = "Hello World& ...

  4. [Leetcode] Length of last word 最后一个单词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the le ...

  5. 58. Length of Last Word最后一个单词的长度

    [抄题]: [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: "b a " 最后一位是空格,可能误 ...

  6. 058 Length of Last Word 最后一个单词的长度

    给定一个字符串, 包含大小写字母.空格 ' ',请返回其最后一个单词的长度.如果不存在最后一个单词,请返回 0 .注意事项:一个单词的界定是,由字母组成,但不包含任何的空格.案例:输入: " ...

  7. Length of Last Word输出最后单词的字母个数

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  8. [leetcode]58. Length of Last Word最后一个词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  9. [Swift]LeetCode58. 最后一个单词的长度 | Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

随机推荐

  1. VS2012 OpenCV2.4.9 Debug可以允许,Release不可以

    一个简单的程序 #include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgu ...

  2. [荐]使用jQuery清空file文件域

    file是文本域,我们一般都会使用它来上传文件,在上传文件时我们需要验证,验证完成后,如果存在错误,为了防止将错误信息也上传上去,我们总是希望能够将其清空.但是在IE中,为了安全起见它是不允许我们改变 ...

  3. Android之TabHost布局(转)

    1.概念 盛放Tab的容器就是TabHost.TabHost的实现有两种方式: 第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab ...

  4. Element selector doesn't have required

    这个错误是因为创建xml文件时文件类型弄成了layout xml file ,这样就会自动到layout文件夹下 应该是drawable resource file

  5. Process32First 返回FALSE的原因

    一般情况下是不会返回FALSE的,如果发生了,请检查: 1:系统为UNICODE的,一定要设置PROCESSENTRY32的dwSize为sizeof(PROCESSENTRY32)即可..

  6. 定时备份mysql

    @echo offset filename=%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2%mysqldump -- ...

  7. bat

    1.输出系统时间,利用系统时间做文件名 @echo offset filename=%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%ti ...

  8. Big Event in HDU

    Description Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe ...

  9. windows下自动关机

    定时:at 00:00 shutdown -s     //在00:00时关机 倒计时:shutdown -s -t 3600   //3600s后关机 取消:shutdown -a

  10. 用padding与margin做多个元素的等间距分布

    这样做的好处是不管有多少个元素等间距分布,都可以直接写在li中,而且由于是给a设定的样式,所以在字数不一致的情况下,样式仍然是统一的. html: <!DOCTYPE html> < ...