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. Google高级技巧—google Hack★★★★

    google hacking事实上并算不上什么新东西,当时并没有重视这样的技术,觉得webshell什么的,并无太大实际用途.google hacking事实上并非如此简单... 经常使用的googl ...

  2. Java加密算法 AES

    版权声明:本文为博主原创文章,转载请注明出处. [java] view plain copy print?在CODE上查看代码片派生到我的代码片 package com.stone.security; ...

  3. RHCA学习笔记:RH442-Unit8进程与调度

      UNIT 8  Processes and the Scheduler 进程与调度 学习目标 A. CPU cache 与Service time之间的关系 B. 分析应用程序使用CPU cach ...

  4. 通过ftp模拟网盘

    package com.xiaomo.ftp.down_upload; import java.io.IOException; import java.util.ArrayList; import j ...

  5. win7 设置自动关机

    1.C:\Windows\System32\shutdown.exe 2. -s:表示关机: -r:表示重启: -t:表示时间,以秒为单位: -a:表示取消shutdown计划,即表示取消关机或重启命 ...

  6. 不支持的关键字:“provider connection string”报错信息及解决方案

    今天在部署公司开发框架的时候 ,登录系统之后调用代办列表的时候就报错了 总线调用契约XX.Service.Contracts.IXXService上的GetXXCount方法时出错. Resoluti ...

  7. android使用模拟机测试时,若要联网IP地址该怎么写?

    android使用模拟机测试时,如果服务器也是本机,那么IP地址如果写为localhost或者127.0.0.1,这样其实是不能访问到本机上部署的服务端,那么该怎么写呢?很简单,把IP地址改为10.0 ...

  8. 重温css系列01

    2016-01-07——解决背景层透明度的问题 需要ie9+ 问题:如果我对div设置opacity: 0.8;这个透明属性后 希望内容不发生改变怎么弄? A:做两层,或者rgba 解决后的效果图: ...

  9. Unity3D 之2D动画机

    这里来讲解一下2D动画机的使用 2D的时候,默认的情况下,可以调用默认的站立之类的动画,然后通过触发,可以变化自己的动画. 一:将一个图切成一些一个元素 二:创建一个精灵,给精灵添加一个动画机 三:给 ...

  10. Chrome资源加载被Cancel的问题

    好个表久没写文章了. 做为一个砖业的砖工只能天天搬砖做些没有营养没有技术难度的活儿. 最近折腾个网站.发现一个很奇怪的问题- 各种图片的请求被取消了状态为Canceled. 顿时Chrome变成一个更 ...