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里。

  1. class Solution {
  2. public:
  3. int lengthOfLastWord(string s) {
  4. const unsigned int len = s.size();
  5. unsigned int head,last,lastlen;
  6. head = ;
  7. last = ;
  8. lastlen = ;
  9. if (!len)
  10. ;
  11. ) {
  12. // search for the first non-space character
  13. while ((s[head] == ' ') && (head != len))
  14. head ++;
  15. if (head == len)
  16. return lastlen;
  17. last = head
  18. while ((last != len) && (s[last] != ' '))
  19. last ++;
  20. if (last == len)
  21. return last - head;
  22. else {
  23. lastlen = last - head;
  24. head = last;
  25. }
  26. }
  27. }
  28. };

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. java08 Set

    Set: 无序不可重复,重复则覆盖,判断是不是重复也是通过equals方法判断的.HashSet和TreeSet,HashSet底层是HashMap. public static void main( ...

  2. android学习资料

      在线查看android源码 1. https://github.com/android 2. http://grepcode.com/project/repository.grepcode.com ...

  3. Activity 与ListActivity的区别

    转载自 http://www.cnblogs.com/bravestarrhu/archive/2012/05/06/2486703.html

  4. [转]Form Builder:app_field.clear_dependent_fields和APP_FIELD.set_dependent_field的用法

    转自:http://www.cnblogs.com/toowang/p/3668070.html 可以调用APP_FIELD.clear_dependent_fields和APP_FIELD.set_ ...

  5. mysql数据库时间、字符串类型互转

    时间格式转换: select DATE_FORMAT(NOW(),"%y-%m-%d %H:%i:%s") 字符串转时间: select STR_TO_DATE("201 ...

  6. C++ 全排列函数 nyoj 366

    C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...

  7. com.android.builder.packaging.DuplicateFile

    解决方法:     packagingOptions {        exclude 'META-INF/DEPENDENCIES'        exclude 'META-INF/NOTICE' ...

  8. c语言学习之基础知识点介绍(十九):内存操作函数

    一.malloc函数 /* 首先需要导入头文件 #include <stdlib.h> malloc void* malloc(n); n是字节大小 开辟堆空间,开辟的字节数以n为准 返回 ...

  9. ThinkPad E40无线网卡驱动安装 FOR CENTOS6.3

    1.看一下咱们用的本本的无线是咋子无线网卡,如下: [root@liaohg Downloads]# lspci | grep Wireless 03:00.0 Network controller: ...

  10. 关于使用用友华表Cell控件按需打印行的方法

    分享下只需一个cll文件按需打印行的觉得最好的方式:1.cell文件要打印行的地方最好不要全删了,留一行,设置好单元格样式(字体.对齐方式.折行自适应等),后面会省一些代码: 2.使用CopyRang ...