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. phpmailer使用gmail SMTP的方法

    终于能够通过phpmailer使用gmail账号发送邮件了phpmailer(现在的版本是1.73)是一个很好用的工具,可以很方便的使用php语言发送邮件,支持smtp及验证,我们一直都用它. 但是, ...

  2. mysql init_connect 参数的其他用处

    http://blog.itpub.net/133735/viewspace-691196/   init_connect 是可以动态在线调整的,这样就有了一些其他的用处 经过测试init_conne ...

  3. Linux下history命令详解---转载

    Linux下History命令主要用于显示历史指令记录内容, 下达历史纪录中的指令 . >History命令语法:[www.linuxidc.com@linux]# history [n][ww ...

  4. shell 实例脚本

    例1: #!/bin/bashsum=0;for i in {1..100..2}do let "sum+=i"doneecho "the sum is $sum&quo ...

  5. ant来历

    看看<ant权威指南>或者ant的网站吧,里面介绍得非常详细ant的来历,目的.ant是tomcat的一个副产品,是在开发tomcat过程中,开发人员需要一个整合编译工具,因为在一个大项目 ...

  6. js操纵cookie技术

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  7. C#汉字转拼音首字母

    输入汉字,提取其首字母: /// <summary> /// 汉字转拼音缩写 /// Code By /// 2004-11-30 /// </summary> /// < ...

  8. jQuery滑过头像图片展示个人信息效果

    这是一款经典的jQuery图片插件,同时,也可以是一款jQuery提示框插件.这款jQuery插件的功能是当你把鼠标滑过头像图片缩略图时,即可弹出头像对应用户的详细个人信息,弹出的标签虽然不大,但是还 ...

  9. 使用T4模板为EF框架添加实体根据数据库自动生成字段注释的功能

    转自http://jeffblog.sinaapp.com/archives/501 首先我们先下载一个文件GetSummery,这里我提供了,大家可以直接下载:下载 我们在数据库建立一个表,并给表中 ...

  10. ACM/ICPC ZOJ1006-Do the Untwist 解题代码

    #include <iostream> #include <string> #include <stdlib.h> using namespace std; int ...