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. js中对arry数组的各种操作小结

    最近工作比较轻松,于是就花时间从头到尾的对js进行了详细的学习和复习,在看书的过程中,发现自己平时在做项目的过程中有很多地方想得不过全面,写的不够合理,所以说啊,为了在以后的工作中写出最优化的代码,我 ...

  2. 解锁Dagger2使用姿势(一)

    毫无疑问,Dagger2的 上手是有门槛的,有门槛是因为它里边的概念多,用起来复杂,可是一旦你学会了Dagger2的使用,你一定会爱不释手的.与ButterKnife和AndroidAnnotatio ...

  3. Linux学习新篇——常用命令和快捷键总结

    最近刚接触Linux,整理了一些常用的命令和快捷键 Tab补全命令 当命令记不清了,输入记得的前几个用Tab就可以将该命令自动补全. 启动tomcat服务用$startup.sh 停止tomcat服务 ...

  4. jQuery多图上传Uploadify插件使用及传参详解

    因为工作需要,这两天接触到了Uploadify插件,由于是第一次用,花了我近一天的时间.下面我把我在用这个插件过程详细的分享出来,也让自己巩固一下,也希望能帮助到你. 所需文件: jquery-1.8 ...

  5. Oracle删除多张表

    项目中遇到要删除多张表,发现不能同时删除,可以先查询出SQL语句,然后批量执行 1.查询出SQL语句: select 'drop table '||table_name || ';' from use ...

  6. Java 内存分析图

    client -------------------------- public class Client{ public static void main(String[] args){ Perso ...

  7. asp.net 开发问题:Web 服务器上的请求筛选被配置为拒绝该请求,因为内容长度超过配置的值。

    "Web 服务器上的请求筛选被配置为拒绝该请求,因为内容长度超过配置的值." 这个问题在开发需要上传文件的时候可能会遇到,今天遇到这个问题,百度过也有挺多的修改方法. 方法1: 修 ...

  8. Android 设置ListView不可滚动 及在ScrollView中不可滚动的设置

    http://m.blog.csdn.net/blog/yusewuhen/43706169 转载请注明出处: http://blog.csdn.net/androiddevelop/article/ ...

  9. .net的 async 和 await

    async 和 await 出现在C# 5.0之后,关系是两兄弟,Task是父辈,Thread是爷爷辈,这就是.net 多线程处理的东西,具体包括 创建线程,线程结果返回,线程中止,线程中的异常处理 ...

  10. MVC小系列(三)【MVC的分部视图】

    MVC的分部视图: 分部视图在action中返回一定要用PartialView(),而不要偷懒使用View(),因为如果使用后者,系统会认为是一个标准视图,会为它加个默认的母版页(LayOut),除非 ...