[LintCode] Length of Last Word 求末尾单词的长度
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.
Notice
A word is defined as a character sequence consists of non-space characters only.
Given s = "Hello World", return 5.
LeetCode上的原题,请参见我之前的博客Length of Last Word。
解法一:
class Solution {
public:
/**
* @param s A string
* @return the length of last word
*/
int lengthOfLastWord(string& s) {
if (s.empty()) return ;
int res = ;
if (s[] != ' ') res = ;
for (int i = ; i < s.size(); ++i) {
if (s[i] != ' ') {
if (s[i - ] == ' ') res = ;
else ++res;
}
}
return res;
}
};
解法二:
class Solution {
public:
/**
* @param s A string
* @return the length of last word
*/
int lengthOfLastWord(string& s) {
int tail = s.size() - , res = ;
while (tail >= && s[tail] == ' ') --tail;
while (tail >= && s[tail] != ' ' ) {
--tail;
++res;
}
return res;
}
};
[LintCode] Length of Last Word 求末尾单词的长度的更多相关文章
- [LeetCode] Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [LeetCode] 58. Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- lintcode:Length of Last Word 最后一个单词的长度
题目: 最后一个单词的长度 给定一个字符串, 包含大小写字母.空格' ',请返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 样例 给定 s = "Hello World& ...
- [Leetcode] Length of last word 最后一个单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the le ...
- 58. Length of Last Word最后一个单词的长度
[抄题]: [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: "b a " 最后一位是空格,可能误 ...
- 058 Length of Last Word 最后一个单词的长度
给定一个字符串, 包含大小写字母.空格 ' ',请返回其最后一个单词的长度.如果不存在最后一个单词,请返回 0 .注意事项:一个单词的界定是,由字母组成,但不包含任何的空格.案例:输入: " ...
- Length of Last Word输出最后单词的字母个数
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [leetcode]58. Length of Last Word最后一个词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [Swift]LeetCode58. 最后一个单词的长度 | Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
随机推荐
- Mesa 3D
Mesa 3D是一个在MIT许可证下开放源代码的三维计算机图形库,以开源形式实现了OpenGL的应用程序接口. OpenGL的高效实现一般依赖于显示设备厂商提供的硬件,而Mesa 3D是一个纯基于软件 ...
- codeforce ABBYY Cup 3.0 - Finals (online version) B2. Shave Beaver! 线段树
B2. Shave Beaver! The Smart Beaver has recently designed and built an innovative nanotechnologic a ...
- Jmeter之安装(一)
Jmeter Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试领域. 小七这边之前用jmeter ...
- SQL.WITH AS.公用表表达式(CTE)(转)
一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候,是 ...
- SpringMyBatis解析1-使用示例
MyBatis使用介绍 MyBatis的详细使用介绍 http://www.cnblogs.com/xrq730/category/796495.html 建立PO public class Per ...
- express-21 静态内容
静态内容是指应用程序不会基于每个请求而去改变的资源. 多媒体: 图片.视频和音频文件 CSS: JavaScript 二进制下载文件: 这包含所有种类:PDF.压缩文件.安装文件等类似的东西. 借助一 ...
- vs 颜色设置
工具-选项-字体和颜色:在项背景点击自定义-色调85 饱和度123 亮度205, 字体则是选择Calibri,个人认为看起来非常舒服.前景字体我选择了偏紫色,会很搭配背景浅绿色以及不会和关键字颜色搞混 ...
- MACD 基本用法
基本用法 1. MACD 金叉:DIFF 由下向上突破 DEA,为买入信号. 2. MACD 死叉:DIFF 由上向下突破 DEA,为卖出信号. 3. MACD 绿转红:MACD 值由负变正,市场由空 ...
- 经典的nav导航
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Thread 的使用
对于Thread 的使用,我要注意的是我经常忽略".start()".之前由于在android开发中,如果是使用网络加载的功能,这个部分需要新增线程,不能在主线程使用. 然后注意要 ...