Leetcode Problem.58—Length of Last Word
2015-06-03 15:18
323 查看
Given a string s consists of upper/lower-case alphabets and empty space characters
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 =
return
My C++ solution!
' ', 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.
My C++ solution!
int lengthOfLastWord(string s) { int len=s.length(); int pos=0; int count=0; if(len==0) return 0; while(pos<len) { if(s[pos]!=32) { count++; pos++; } else { while(pos<len&&s[pos]==32) pos++; if(pos<len) { count=1; pos++; } } } return count; }
相关文章推荐
- Leetcode Problem.28—Implement strStr()
- JAVA中正则表达式总结(详解及用途)
- JSON 数据列表取值
- 关于android sqlite 加密的研究
- MySQL用触发器同步两张表
- 补码总结
- 前端学习笔记之页面制作(一)——PS切图
- 获取iphone的设备信息
- iOS 7 SDK: 如何使用后台获取(Background Fetch)
- Hadoop中的辅助类ToolRunner和Configured的用法详解
- 1156: 钟
- iOS生成随机数
- oracle 11g expdp impdp详细使用方法
- 【leetcode c++】twoSum
- Android SlidingMenu 使用详解
- 前端学习笔记之页面制作(三)——HTML
- 前端学习笔记之页面制作(二)——开发环境Sublime Text2(持续更新)
- 我们常用的测试管理工具—JIRA篇
- 里程计、推算定位与视觉里程计
- 关于FVID2_Frame结构体的一点疑惑