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. 原创C# 枚举 多状态 操作

    C# 中枚举类型是一种值类型,目前(vs2012)还不能用于泛型. 此类型最多的用处是标识一组相同类型的状态量或常量,比如: 状态量 示例一 [Flags] public enum Connectio ...

  2. Java基础知识强化之集合框架笔记74:各种集合常见功能 和 遍历方式总结

    1. Collection add() remove() contains() iterator() size() 遍历: 增强for 迭代器 |--List get() 遍历: 普通for |--S ...

  3. C#微信公众号开发 -- (六)自定义菜单事件之CLICK

    微信公众号中当用户手动点击了按钮,微信公众号会被动的向用户发送文字消息或者图文消息. 通过C#微信公众号开发 -- (五)自定义菜单创建 我们知道了如何将CLICK类型的按钮添加到自己的微信公众平台上 ...

  4. CentOS 6.4安装OpenOffice

    终端依次输入: (1)sudo yum install openoffice.org-writer (2) sudo  yum install openoffice.org-calc (3) sudo ...

  5. 火狐浏览器插件Modify Headers伪造IP地址

    安装插件:先打开火狐浏览器 => 找到下载好的 modify_headers.xpi 插件文件 => 鼠标按住插件文件不放,拖拽到火狐浏览器界面 => 按提示重启浏览器 => ...

  6. ios ReactiveViewModel

    项目中使用 ReactiveCocoa 一般都会嵌入ReactiveViewModel 或者 ReactiveCocoaLayout 联合处理UI.网络.动画.布局.窗口切换等,组合使用时威力惊人. ...

  7. input设置disabled,经过strus2提交到后台,后台取不到值

    页面中有多个name相同的input与后台action中一个属性对应,一直在好奇为什么会可以提交到后台呐,但是有时还报这个属性找不到对应的方法(多个name相同好像匹配的是数组,所以找不到), 但是我 ...

  8. IOS 学习日志 2015-3-16

    Objective--C 一 关键字 self 相当于java中的this,但是又有不同 它即可一代替对象,也可以代替类, 也就是说它既可以用在静态方法中又可以用在动态方法中. super 相当于父类 ...

  9. 【ADO.NET】6、SQLHelper简单封装

    using System.Data.SqlClient;using System.Configuration;引用:System.Configuration 连接字符串放到配置文件中 新建一个类,写如 ...

  10. Django 创建第一个项目(转)

    转自(http://www.runoob.com/django/django-first-app.html) 前面写了不少python程序,由于之前都是作为工具用,所以命令行就足够了,最近写的测试用例 ...