58. Length of Last Word
2016-07-20 14:55
330 查看
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 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.
思路:倒着遍历字符串
class Solution { public: int lengthOfLastWord(string s) { int len = 0, tail = s.length() - 1; while (tail >= 0 && s[tail] == ' ') tail--; while (tail >= 0 && s[tail] != ' ') { len++; tail--; } return len; } };
相关文章推荐
- eclipse查看jar包源代码乱码问题解决
- <C++略识>之对象数组与对象成员
- ios 充值测试
- Codeforces-698A Vacations
- [置顶] 二维码的生成
- 浅谈LCA的离线算法
- Python 黑帽编程大纲(变化中)
- js 插件 弹出deom 自身属性值
- 博弈论
- Mybatis insert返回主键ID
- 和安全有关的那些事(非对称加密、数字摘要、数字签名、数字证书、SSL、HTTPS及其他)
- css中的border-image的用法
- 在java 用xml模板通过freemarker生成word文档
- Java类加载分析
- WCF入门(十二)---WCF异常处理
- 【安装禅道和redmine的简单过程】
- Windows根据端口号杀死进程
- Packets
- Head First Java笔记(六)
- 【MarkdownPad专业版】MarkdownPad2.4.2专业版破解教程以及应用下载