leetcode 58:Length of Last Word
2015-11-18 20:49
519 查看
题目:
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
思路:
额,这题比较简单,从最后一个元素开始往前扫描就行啦,刚开始跳过空格。
实现:
class Solution {
public:
int lengthOfLastWord(string s) {
int size = s.size();
int i = size-1;
while (i >= 0 && s[i] == ' ') --i;
if (i < 0) return 0;
int j = i;
for (; j >= 0 && s[j] != ' '; --j){}
return (i-j);
}
};
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.
思路:
额,这题比较简单,从最后一个元素开始往前扫描就行啦,刚开始跳过空格。
实现:
class Solution {
public:
int lengthOfLastWord(string s) {
int size = s.size();
int i = size-1;
while (i >= 0 && s[i] == ' ') --i;
if (i < 0) return 0;
int j = i;
for (; j >= 0 && s[j] != ' '; --j){}
return (i-j);
}
};
相关文章推荐
- 树莓派2代B model 上手初体验,不用显示器,Python GPIO 点亮一颗LED
- 能源互联网“十三五”迎机遇 发展面临三大障碍
- 在 Lua 里 使用 Cocos Studio 导出的 .csb 文件
- POJ - 3662 Telephone Lines
- 行计数
- java IO流的简单介绍及应用
- 【试错】——人类不断学习、进步的前提
- [IOS开发]模仿微博的客户端(1)
- iOS多线程技术
- CSS利用checkbook实现开关按钮
- 圈奶牛<计算几何><C++>
- 1.Android AlertDialog
- C++语法疑点
- C#中abstract和virtual区别
- Android使用SVG矢量图打造酷炫动效!
- 大数据下union all的视图分页解决方案
- java
- 安卓开发实例二:文件浏览器
- dota BP练习工具开发:一个C/S多用户程序
- 从头认识java-9.7 LinkedList