LeetCode 58 Length of Last Word
来源:互联网 发布:c语言3右移移运算4 编辑:程序博客网 时间:2024/04/19 16:05
题目
分析
之前想的比较简单:从后往前遍历找到一个空格的地方,返回之间的差值。
但是由于这个题例子的刁钻程度是不能行的,于是就先找字母出现的地方做个标记,再找空格出现的地方,返回差值。
题解
class Solution {public: int lengthOfLastWord(string s) { int flag=0; int tail; for(int i=s.size()-1;i>=0;i--){ if(s[i]>='A' && s[i]<='z') { if(flag==0) { flag=1; tail=i; } } else if(s[i]==' ') { if(flag==1) return tail-i; } } if(flag==1) return tail+1; //找到字符后再也没找到空格 "abc " else return 0; }};
0 0
- [leetcode 58] Length of Last Word
- leetcode-58 Length of Last Word
- Leetcode NO.58 Length of Last Word
- LeetCode #58 length of Last Word
- [LeetCode 58]Length of Last Word
- leetcode || 58、Length of Last Word
- LeetCode 58 Length of Last Word
- [Leetcode 58, easy] Length of Last Word
- Length of Last Word (leetCode 58)
- leetcode[58]:Length of Last Word
- 【leetcode c++】58 Length of Last Word
- leetcode 58:Length of Last Word
- LeetCode---(58)Length of Last Word
- leetcode-58-Length of Last Word
- Leetcode# 58 Length of Last Word
- [leetcode-58]Length of Last Word(c)
- leetcode 58: Length of Last Word
- leetCode #58 Length of Last Word
- Linux中可自动补齐命令
- java基础--面向对象(1)
- 黑马程序员---Java 学习笔记
- Apache shiro集群实现 (二) shiro 的INI配置
- hdu2112 HDU Today(最短路)
- LeetCode 58 Length of Last Word
- 生物语料词性标注工具——genia tagger
- 转Android性能调优
- 【转】GitHub秘籍
- 深度分析Word2vec 笔记 (必须收藏)
- SQL基础--> 序列(SEQUENCE)、同义词(SYNONYM)
- 【数据库系列(一)】基本操作
- HDU 1387 Team Queue
- NSDictionary - NSData - NSString conversion