Leetcode Length of Last Word
来源:互联网 发布:mac怎么输入大写字母 编辑:程序博客网 时间:2024/09/21 06:19
一开始以为如果最后一个字符是space就不存在最后一个单词,WA了一次。
遍历该string,cnt记录当前单词的长度,如果遇到space,还要判断它的下一个字符是不是space,是的话就重置cnt为0,否则不变。
class Solution {public: int lengthOfLastWord(const char *s) { int len = strlen(s); int cnt = 0; for(int i = 0; i < len; i++){ if(s[i] == ' ' && i != len-1 && s[i+1] != ' '){ cnt = 0; } else if(s[i] != ' '){ cnt++; } } return cnt; }};
- 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 线程同步
- 套接字IO模型
- java.lang.OutOfMemoryError: Java heap space
- Python - while语句和if语句 的 用法 及 代码
- a href链接弹出新窗口的方法
- Leetcode Length of Last Word
- iOS开发网站链接
- 一些数学知识集合
- JAVA中String的split
- N阶楼梯上楼问题
- 三个水杯(改进)
- Ubuntu下MSP430开发环境搭建
- INFO: Illegal access: this web application instance has been stopped already. Could not load
- 基于visual Studio2013解决C语言竞赛题之1004平均值