LeetCode:Length of Last Word
来源:互联网 发布:怎么让淘宝店铺关掉 编辑:程序博客网 时间:2024/06/05 20:00
Given a string s consists of upper/lower-case alphabets and empty space characters ' '
, 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 ”通过不了,最后在for循环之前加了一个while先去掉最后的空格 通过。
class Solution {public: int lengthOfLastWord(string s) { int num=0; if (s.length()) { int r = s.length()-1; while (s[r]==' ') { r--; } for ( ; r>=0; r--) { if(s[r]==' '){ 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
- 从C++到COM,学习笔记(3)
- HDU 1698 Just a Hook (线段树区间修改)
- CSU_1511_残缺的棋盘
- 数组非数字键名引号的必要性
- C/C++ 字符串操作1---循环移位
- LeetCode:Length of Last Word
- WINDOWS安装python3.X遇到的错误解决方法
- BN Topic Model 中如何判断conditional independence p(x,y|z)
- PHP CLI模式下的多进程应用
- 启动页面加载
- Java并发教程(Oracle官方资料)
- JAVA_SE系列:06.文档注释和API文档
- OpenStack之日志
- hdu1890 Robotic Sort Splay树,区间反转,lazy标记