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] 58. Length of Last Word 求末尾单词的长度的更多相关文章

  1. [LeetCode] Length of Last Word 求末尾单词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  2. [LintCode] Length of Last Word 求末尾单词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  3. [leetcode]58. Length of Last Word最后一个词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  4. 58. Length of Last Word最后一个单词的长度

    [抄题]: [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: "b a " 最后一位是空格,可能误 ...

  5. LeetCode 58. Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  6. Java [Leetcode 58]Length of Last Word

    题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...

  7. leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法

    Length of Last Word  Given a string s consists of upper/lower-case alphabets and empty space charact ...

  8. [Leetcode] Length of last word 最后一个单词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the le ...

  9. lintcode:Length of Last Word 最后一个单词的长度

    题目: 最后一个单词的长度 给定一个字符串, 包含大小写字母.空格' ',请返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 样例 给定 s = "Hello World& ...

随机推荐

  1. JavaScript(这里主要侧重于 JavaScript HTML DOM)杂项

    JavaScript基础 ------学习网站https://www.runoob.com/js/js-tutorial.html Javascript语言的特点: 安全性:2.易用性:3.动态交互性 ...

  2. Visual Studio 2019 (VS2019)正式版安装 VisualSVN Server 插件

    VS2019 正式版最近刚刚推出来,目前 Ankhsvn 还不支持,它最高只支持 VS2017,全网搜索了一下,也没有找到.在 Stackoverflow 上看了一下,找到这篇问答: 自己按照这种方法 ...

  3. WPF Xaml中创建集合

    首先在xaml中创建集合是一个不可取的方法. 本方法仅作为xaml的学习. 本文略微无聊,主要是编写的东西都是老玩意. 首先是定义一个类,作为你要加载集合的模型. 结构如下 internal clas ...

  4. docker命令之link

    1.新建两台容器,第二台(busybox_2)link到第一台(busybox_1) [root@localhost ~]# docker run -d -it --name busybox_1 bu ...

  5. 机器学习(八)--------支持向量机 (Support Vector Machines)

    与逻辑回归和神经网络相比,支持向量机或者简称 SVM,更为强大. 人们有时将支持向量机看作是大间距分类器. 这是我的支持向量机模型代价函数 这样将得到一个更好的决策边界 理解支持向量机模型的做法,即努 ...

  6. MySQL for OPS 05:日志管理

    写在前面的话 日志是作为用户排查服务问题的重要依据,在 MySQL 中日志可以分为几类,各自产生着不同的作用.如 error log / bin log / slow log 等.很多时候优化数据库的 ...

  7. Maven配置教程详解

    Maven的安装与配置 一.在https://www.cnblogs.com/zyx110/p/10799387.html中下载以下maven安装包 解压缩即可 根据你的安装路径配置maven环境变量 ...

  8. SetApartmentState(ApartmentState state).Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process

    System.Threading.ThreadStateException: 'Current thread must be set to single thread apartment (STA) ...

  9. python匹配ip地址

    ip地址是用3个'.'号作为分隔符,分割4个数字,每个数字的取值在[0,255],一般日志文件中的ip地址都是有效的ip地址,不需要我们再去验证,因此,若从日志文件中提取ip,那么可以简单写成这样: ...

  10. 解决:500 Internal Privoxy Error

    500 Internal Privoxy Error Privoxy encountered an error while processing your request: Could not loa ...