58. Length of Last Word
2016-07-07 12:15
302 查看
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
注意这样的“a ”,最后一个字符串是a。所以先要去掉末尾的空格。
class Solution {
public:
int lengthOfLastWord(string s) {
int cnt = 0;
int len = s.size()-1;
while (len>=0&&s[len] == ' '){//去掉末尾的空格
len--;
}
while(len>=0&&s[len]!=' '){
len--;
cnt++;
}
return cnt;
}
};
' ', 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.
注意这样的“a ”,最后一个字符串是a。所以先要去掉末尾的空格。
class Solution {
public:
int lengthOfLastWord(string s) {
int cnt = 0;
int len = s.size()-1;
while (len>=0&&s[len] == ' '){//去掉末尾的空格
len--;
}
while(len>=0&&s[len]!=' '){
len--;
cnt++;
}
return cnt;
}
};
相关文章推荐
- Android应用程序通用自动脱壳方法研究
- 为什么在检测颜色信息时往往用HSV空间而不用RGB空间呢?
- User Story的常见困难
- Mac上搭建直播服务器Nginx+rtmp
- git学习与使用
- Android ADT修改主题配色方案 Color theme
- VOIP音视频带宽计算
- C语言标准
- MSSQL查询重复,删除重复
- qsdk编译
- 查看 tomcat是32位或是64位的版本
- 真实现场大还原!QUHD最严考试到底经历了什么?
- Release from App standby(Android M)
- Fragment 切换的两种方式
- Linux中安装使用RXVT命令行终端窗口程序的教程
- Three.js(3)
- 算法_符号表
- 深入浅出ES6(四):模板字符串
- 绝对的真爱
- 纯干货:Android APP 防破解进化史