Length of Last Word 最后一个单词的长度
2015-12-04 15:27
447 查看
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
' ', 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) { if(s == ""){ return 0; } int len = s.length()-1; while(s[len] == ' ' && len >= 0){ len--; } int sum = 0; for(int i=len;i>=0;i--){ if(s[i] != ' '){ sum++; }else{ break; } } return sum; } };
相关文章推荐
- jquery笔记
- jquery基本效果
- dropdownlist控件的使用(数据绑定)
- 判断mysqli函数里的sql语句是否有错和影响行数
- The beginning iOS8 Programming with Swift 中文翻译 - 4
- iOS开发-地图定位 CoreLocation地理编码
- 内存问题 动态加载地址和运行时地址
- LinearLayout属性orientation
- Fiddler自动响应AutoResponder正则匹配
- Volley关于添加Cookie总结
- srping 注解配置aop例子
- android开发异步加载网络数据AsyncTask的使用
- 使用Spring MVC统一异常处理实战<转>
- tcpdump用法详解,案例分析详解
- java入门练习题一
- 记录下遇到的一个比较坑爹的问题
- linux中send函数MSG_NOSIGNAL异常消息
- 配置Ubuntu Server高速apt-get源
- 安装php7及安装memcache扩展出现的新旧php版本兼容扩展的问题
- 时序图