LeetCode---Length of Last Word
来源:互联网 发布:java linktag.class 编辑:程序博客网 时间:2024/06/05 20:49
题目大意:给出一个字符串,由大小写字母和空格组成,输出最后一个单词的长度,如果单词不存在长度为0。
算法思想:
1.从后向前遍历字符串。
2.忽略掉没有字符串尾部的空格。
3.当扫描过一个单词后遇到空格,终止循环。
代码如下:
class Solution {public: int lengthOfLastWord(string s) { if(s.size()==0) return 0; int num=0; bool flag=false; for(int i=s.size()-1;i>=0;--i){ if(num==0&&s[i]==' ') continue; if(s[i]==' ') break; ++num; } return num; }};
0 0
- LeetCode: Length of Last Word
- LeetCode Length of Last Word
- LeetCode : Length of Last Word
- [Leetcode] Length of Last Word
- [LeetCode] Length of Last Word
- [LeetCode]Length of Last Word
- [leetcode]Length of Last Word
- Leetcode:Length of Last Word
- LeetCode-Length of Last Word
- [LeetCode] Length of Last Word
- [leetcode] Length of Last Word
- leetcode length of last word
- LeetCode - Length of Last Word
- leetcode:Length of Last Word
- LeetCode:Length of Last Word
- LeetCode | Length of Last Word
- LeetCode - Length of Last Word
- Leetcode: Length of Last Word
- JAVA编程规则【转自java编程思想】
- HDU5491 The Next【二进制】
- Leetcode 67:Add Binary
- 安卓控件使用系列15:ImageView适屏和裁剪图片
- HDU 2140 模拟
- LeetCode---Length of Last Word
- 重写ajax方法实现异步请求session过期时跳转登录页面
- JAVA的StringBuffer类
- php yii连接mysql配置扩展
- Linux(Fedora21)安装google chrome浏览器
- IO端口和IO内存的区别及分别使用的函数接口
- 利用DBREF实现MongoDB的引用("外键")
- TL-WR703N刷OpenWrt (by quqi99)
- 数据结构与算法分析(c++版) #13 堆与优先队列