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 strLen=s.size();
int len=0;
int i=0;
while(s[strLen-i-1]==' ')
i++;
for(;i<strLen;i++)
{
if(s[strLen-i-1]>='A'&&s[strLen-i-1]<='Z' || s[strLen-i-1]>='a'&&s[strLen-i-1]<='z' )
len++;
else
break;
}
return len;
}
};

或者用STL来写:

class Solution {
public:
int lengthOfLastWord(string s) {
int i = s.find_last_not_of(' ');
return (i == string::npos) ? 0 : (i - s.find_last_of(' ', i));
}
};

注:本博文为EbowTang原创。兴许可能继续更新本文。假设转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50498956

原作者博客:http://blog.csdn.net/ebowtang

&lt;LeetCode OJ&gt; 58. Length of Last Word的更多相关文章

  1. LeetCode练题——58. Length of Last Word

    1.题目 58. Length of Last Word——Easy Given a string s consists of upper/lower-case alphabets and empty ...

  2. 【leetcode❤python】 58. Length of Last Word

    #-*- coding: UTF-8 -*-#利用strip函数去掉字符串去除空格(其实是去除两边[左边和右边]空格)#利用split分离字符串成列表class Solution(object):   ...

  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. 【一天一道LeetCode】#58. Length of Last Word

    一天一道LeetCode系列 (一)题目 Given a string s consists of upper/lower-case alphabets and empty space charact ...

  5. 【LeetCode】58. Length of Last Word 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 双指针 单指针 日期 题目地址:https: ...

  6. LeetCode 58. Length of Last Word

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

  7. 【LeetCode】58 - Length of Last Word

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

  8. Java [Leetcode 58]Length of Last Word

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

  9. 58. Length of Last Word【leetcode】

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

随机推荐

  1. ASP.NET没有魔法——ASP.NET Identity与授权

    一个完整的ASP.NET的请求中会存在身份验证(Authentication)阶段以及授权(Authorization)阶段,英文单词Authentication和Authorization非常相似, ...

  2. C#的Random到底该怎么使用

    先看代码: 在循环中,有的只NEW一个Random,有的每次都NEW 一个Random. Console.WriteLine("1.多个Random,默认随机种子,"); ; i ...

  3. MongoDB的mongos实例因无法分配mlock内存挂掉

    问题版本 mongodb-v3.4.4 问题描述 mongos两天死了两次,死前遗言只有日志: 2017-11-01T11:25:27.135+0800 F - [NetworkInterfaceAS ...

  4. x01.AntWorld: An Python AI Game

    1. 学习了一下 AI 五子棋,顺手改作 19 路的棋盘,便于围棋通用.render.py 主要修改如下: # 常量部分: IMAGE_PATH = 'img/' StoneSize = 32 WID ...

  5. [最短路][部分转]P1027 Car的旅行路线

    题目描述 又到暑假了,住在城市A的Car想和朋友一起去城市B旅游.她知道每个城市都有四个飞机场,分别位于一个矩形的四个顶点上,同一个城市中两个机场之间有一条笔直的高速铁路,第I个城市中高速铁路了的单位 ...

  6. npoi导入导出

    NPOI是指构建在POI 3.x版本之上的一个程序,NPOI可以在没有安装Office的情况下对Word或Excel文档进行读写操作. NPOI是一个开源的Java读写Excel.WORD等微软OLE ...

  7. unity android相互调用

    简介 有一些手机功能,Unity没有提供相应的接口,例如震动,例如不锁屏,例如GPS,例如... 有太多的特殊功能Unity都没有提供接口,这时候,我们就需要通过使用Android原生的ADT编辑器去 ...

  8. 关于苹果APP的上架整理

    由于苹果的机制,在非越狱机器上安装应用必须通过官方的App Store,开发者开发好应用后上传App Store,也需要通过审核等环节.AppCan作为一个跨主流平台的一个开发平台,也对ipa包上传A ...

  9. Http简单思维导图

  10. Android 软键盘的显示和隐藏,这样操作就对了

    一.前言 如果有需要用到输入的地方,通常会有需要自动弹出或者收起软键盘的需求.开篇明义,本文会讲讲弹出和收起软键盘的一些细节,最终还会从源码进行分析. 想要操作软键盘,需要使用到 InputMetho ...