首页 > 其他 > 详细

LeetCode 58 Length of Last Word(最后单词的长度)

时间:2016-01-27 13:04:00      阅读:133      评论:0      收藏:0      [点我收藏+]

翻译

给定一个包含大小写字母和空格字符的字符串,

返回该字符串中最后一个单词的长度。

如果最后一个单词不存在,返回0。

批注:
一个单词被定义为包含非空字符的字符序列。

例如,
给定 S = "Hello World",
返回 5

原文

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.

分析

灵活使用find函数套装……

技术分享

int lengthOfLastWord(string s) {
    int index = s.find_last_not_of(‘ ‘);
    return index == -1 ? 0 : index - s.find_last_of(‘ ‘, index);
}

LeetCode 58 Length of Last Word(最后单词的长度)

原文:http://blog.csdn.net/nomasp/article/details/50592917

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!