<LeetCode OJ> 58. Length of Last Word
2016-01-11 19:00
127 查看
58. Length of Last Word
My SubmissionsQuestion
Total Accepted: 78112 Total
Submissions: 275441 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
Hide Tags
String
//思路首先:简单模拟思想,去掉尾部的空格并统计是字符的个数直到再次遇到空格就停止统计 class Solution { public: int lengthOfLastWord(string s) { int strLen=s.size(); int len=0; int i=0; while(s[strLen-i-1]==' ') i++; for(;i<strLen;i++) { if(s[strLen-i-1]>='A'&&s[strLen-i-1]<='Z' || s[strLen-i-1]>='a'&&s[strLen-i-1]<='z' ) len++; else break; } return len; } };
或者用STL来写:
class Solution { public: int lengthOfLastWord(string s) { int i = s.find_last_not_of(' '); return (i == string::npos) ? 0 : (i - s.find_last_of(' ', i)); } };
注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50498956
原作者博客:http://blog.csdn.net/ebowtang
相关文章推荐
- Unity 字符串按自然数排序
- URL中“#” “?” &“”号的作用
- 全文检索引擎Solr系列——入门篇
- Chrome扩展开发之一——Chrome扩展的文件结构
- Upgrade to postgresql 9.5
- 快速计算积分图
- AndroidManifest.xml中一些权限的注释
- iOS第五天 C语言 函数
- maven使用笔记一 下载json-lib引发的问题
- idea中maven的基本配置
- 3、去除字符串中的重复字符
- idea 使用快捷键
- UI基本控件
- LinkedList,ArrayList末尾插入谁效率高?
- Visual Studio中的Build和Rebuild区别
- FSL - MELODIC
- Hashmap原理
- ctags 的安装和使用
- python 中的yield
- QT 编译 undefined reference to `vtable for Class