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.

两个下标,head搜索非空格元素,last搜索head后的第一个空格或者字符串结尾。检测到单词后保存至lastlen里。

class Solution {
public:
    int lengthOfLastWord(string s) {
        const unsigned int len = s.size();
        unsigned int head,last,lastlen;
        head = ;
        last = ;
        lastlen = ;
        if (!len)
            ;
        )    {
            // search for the first non-space character
            while ((s[head] == ' ') && (head != len))
                head ++;
            if (head == len)
                return lastlen;
            last = head;
while ((last != len) && (s[last] != ' ')) last ++; if (last == len) return last - head; else { lastlen = last - head; head = last; } } } };

Length of Last Word | Leetcode的更多相关文章

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

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

  2. leetcode 题解: Length of Last Word

    leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...

  3. LeetCode——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]Length of Last Word @ Python

    原题地址:https://oj.leetcode.com/problems/length-of-last-word/ 题意: Given a string s consists of upper/lo ...

  6. Leetcode(58)题解:Length of Last Word

    https://leetcode.com/problems/length-of-last-word/ 题目: Given a string s consists of upper/lower-case ...

  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. 【LeetCode算法-58/66】Length of Last Word/Plus One

    LeetCode第58题: Given a string s consists of upper/lower-case alphabets and empty space characters ' ' ...

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

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

随机推荐

  1. java23 XML

    XML:可扩展标记语言. xml可以当成一个小型的数据库, html / \ html5 xhtml(格式比较严格) | xml xml解析方式有2种: -SAX,SAX是基于事件流的解析,事件流解析 ...

  2. Mac OS X 如何设置默认浏览器

    有时候我们不希望在 Mac 中点击任何连接都打开的是 Safari,这需要修改默认浏览器设置,在 Mac OS X 中如何设置默认浏览器呢? 打开 Safari 的偏好设置,在「通用」选项卡中有「默认 ...

  3. 【转】Win7环境下VS2010配置Cocos2d-x-2.1.4最新版本的开发环境(亲测)

    http://blog.csdn.net/ccf19881030/article/details/9204801 很久以前使用博客园博主子龙山人的一篇博文<Cocos2d-x win7+vs20 ...

  4. windows 下 node 多版本管理工具 - gnvm

    最近写了各个构建工具, 开发环境为mac,需要在windows下测试通过: 因为很久不用windows,windows下的node 版本还是 0.10.* 的,因此决定升级node mac 下我使用的 ...

  5. 1.Linux系统(CenntOS)固定IP的设定

    首先:我们配置一个临时的固定的固定ip,作用是让我们用其他的shell工具连接虚拟机 ifconfig eth0 192.168.10.168 在主机用ping命令查询连通后再进行下一步 ping 1 ...

  6. C#的语句

    什么是语句?语句可以是以分号结尾的单行代码,或者是语句块中的一系列单行语句.语句块括在括号 {} 中,并且可以包含嵌套块. 1,语句的类型 (1)声明语句 声明语句主要是引入新的变量和常量.变量的声明 ...

  7. jpGraph的应用及基本安装配置 BY 命运

    1.---jpGraph其实就是一个图表类库,会让开发者们作图非常方便,只要几行代码就可以勾画出非常炫的图表. 官方下载地址是:http://jpgraph.net/download/   jpGra ...

  8. SQL Server内连接、外连接、交叉连接

    前言 在数据库查询中,我们常常会用到的表连接查询,而我自己在工作中也是时常用这些表连接查询.而就在刚刚我却还没有搞清楚数据库表连接到底有哪几种, 这几种表连接查询方式又有什么区别,实属惭愧!借以此文以 ...

  9. Android 设计随便说说之简单实践(消息流动)

    在上面两篇分别说明了设计中较为简单也是很关键的实践点. 第一模块划分,它是根据每个模块所承载的业务,进行划分,是应用程序一个静态的描述. 第二合理组合,它是是将每个模块调动起来,共同实现业务,是一个准 ...

  10. 双程动态规划 nyoj61

    题目大意: 在矩阵m*n中,从(1,1)点到(m,n)点,再从(m,n)点到(1,1)点,所走路线经过的同学最大好心值, 要求每个点只能走一遍. 分析: ①我们可以把它只看成两个人同时从(1,1)点, ...