[LeetCode] 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.
Note: A word is defined as a character sequence consists of non-space characters only.
Example:
Input: "Hello World"
Output: 5
这道题难度不是很大。先对输入字符串做预处理,去掉开头和结尾的空格,然后用一个计数器来累计非空格的字符串的长度,遇到空格则将计数器清零,参见代码如下:
解法一:
class Solution {
public:
int lengthOfLastWord(string s) {
int left = , right = (int)s.size() - , res = ;
while (s[left] == ' ') ++left;
while (s[right] == ' ') --right;
for (int i = left; i <= right; ++i) {
if (s[i] == ' ') res = ;
else ++res;
}
return res;
}
};
昨晚睡觉前又想到了一种解法,其实不用上面那么复杂的,这里关心的主要是非空格的字符,那么实际上在遍历字符串的时候,如果遇到非空格的字符,只需要判断其前面一个位置的字符是否为空格,如果是的话,那么当前肯定是一个新词的开始,将计数器重置为1,如果不是的话,说明正在统计一个词的长度,计数器自增1即可。但是需要注意的是,当 i=0 的时候,无法访问前一个字符,所以这种情况要特别判断一下,归为计数器自增1那类,参见代码如下:
解法二:
class Solution {
public:
int lengthOfLastWord(string s) {
int res = ;
for (int i = ; i < s.size(); ++i) {
if (s[i] != ' ') {
if (i != && s[i - ] == ' ') res = ;
else ++res;
}
}
return res;
}
};
下面这种方法是第一种解法的优化版本,由于只关于最后一个单词的长度,所以开头有多少个空格起始并不需要在意,从字符串末尾开始,先将末尾的空格都去掉,然后开始找非空格的字符的长度即可,参见代码如下:
解法三:
class Solution {
public:
int lengthOfLastWord(string s) {
int right = s.size() - , res = ;
while (right >= && s[right] == ' ') --right;
while (right >= && s[right] != ' ' ) {
--right;
++res;
}
return res;
}
};
这道题用Java来做可以一行搞定,请参见这个帖子.
Github 同步地址:
https://github.com/grandyang/leetcode/issues/58
参考资料:
https://leetcode.com/problems/length-of-last-word/
https://leetcode.com/problems/length-of-last-word/discuss/21927/My-3-line-0-ms-java-solution
https://leetcode.com/problems/length-of-last-word/discuss/21892/7-lines-4ms-C%2B%2B-Solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Length of Last Word 求末尾单词的长度的更多相关文章
- [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 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [Leetcode] Length of last word 最后一个单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the le ...
- lintcode:Length of Last Word 最后一个单词的长度
题目: 最后一个单词的长度 给定一个字符串, 包含大小写字母.空格' ',请返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 样例 给定 s = "Hello World& ...
- 058 Length of Last Word 最后一个单词的长度
给定一个字符串, 包含大小写字母.空格 ' ',请返回其最后一个单词的长度.如果不存在最后一个单词,请返回 0 .注意事项:一个单词的界定是,由字母组成,但不包含任何的空格.案例:输入: " ...
- 58. Length of Last Word最后一个单词的长度
[抄题]: [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: "b a " 最后一位是空格,可能误 ...
- C#LeetCode刷题之#58-最后一个单词的长度(Length of Last Word)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3927 访问. 给定一个仅包含大小写字母和空格 ' ' 的字符串, ...
- LeetCode——Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [leetcode]Length of Last Word @ Python
原题地址:https://oj.leetcode.com/problems/length-of-last-word/ 题意: Given a string s consists of upper/lo ...
随机推荐
- const 与 readonly知多少
原文地址: http://www.cnblogs.com/royenhome/archive/2010/05/22/1741592.html 尽管你写了很多年的C#的代码,但是可能当别人问到你cons ...
- MySQL存储过程(转)
一.MySQL 创建存储过程 "pr_add" 是个简单的 MySQL 存储过程,这个存储过程有两个 int 类型的输入参数 "a"."b" ...
- php的http_build_query使用
http_build_query生成 url-encoded 之后的请求字符串 1.使用键值对,关联数组: <?php $data = array('foo'=>'bar', 'baz'= ...
- 使用 ExecuteMultiple 提高批量数据加载的性能
您可以使用 ExecuteMultipleRequest 消息在 Microsoft Dynamics CRM Online 2016 Update 和 Microsoft Dynamics CRM ...
- UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning
#import "ModelAnimationDelegate.h" #import <UIKit/UIKit.h> #import "MapVC.h&quo ...
- iOS报错[__NSCFNumber length]: unrecognized
出现这种报错很大的原因是因为类型给错了,或许你这个数据是从json上解析后得到的,但是需要看一下这个数据是NSString还是NSNumber类型,如果是NSNumber类型的话,你又直接使用NSSt ...
- iOS 语音朗读
//判断版本大于7.0 if ([[[UIDevice currentDevice] systemVersion] integerValue] >= 7.0) { NSStr ...
- iOS NSNotificationCenter详解
通知中心的特点: 1:同步执行 2: 一对多发送消息 3: 降低程序耦合度 通知中心是单例,目的就是从任意一个发送消息到任意一个接收者,是同步执行的. 那么什么是同步呢? 用网上经典的说法,就是我叫朋 ...
- ASP.NET MVC 5 01 - ASP.NET概述
本篇目录: ASP.NET 概述 .NET Framework 与 ASP.NET ASP.NET MVC简介 ASP.NET的特色和优势 典型案例 ▁▃▅ ASP.NET概述 ▅▃▁ 目前开发B/S ...
- SQL Server 如何通过SQL语句定位SSRS中的具体报表
在一些IT技术人员的推广.简单培训后,公司很多部门都有一些非IT技术人员参与开发各自需求的Reporting Service报表.原因很简单,罗列出来的原因大概有这样一些: IT部门的考量: 1:IT ...