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.

我的方法是:

  1. 先消除字符串首、尾的空格;
  2. 再从字符串尾向首查找,直到找到空格或者指针到队首都没找到空格为止,返回最后一个 word 的长度.

我方法,我代码:

\(O(n)\) time, \(O(1)\) extra space.

// 解题关键:消除 s 的首尾空格
int lengthOfLastWord(string s) {
const int n = s.size() - 1;
if (s.size() == 0) return 0; int begin = 0, end = n; //消除字符串首尾空格
if (s[0] == ' ') {
for (int i = 0; i < s.size(); i++) {
if (s[i] != ' ') {
begin = i;
break;
}
}
} if (s[n] == ' ') {
for (int i = n; i >= 0; i--) {
if (s[i] != ' ') {
end = i;
break;
}
}
} // 从尾部开始查找
for (int i = end; i >= begin; i--) {
if (s[i] == ' ') return end - i;
else if (i == begin) return end - begin + 1;
}
}

58. Length of Last Word(easy, 字符串问题)的更多相关文章

  1. LeetCode练题——58. Length of Last Word

    1.题目 58. Length of Last Word——Easy Given a string s consists of upper/lower-case alphabets and empty ...

  2. [LeetCode] 58. Length of Last Word 求末尾单词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  3. LeetCode 58. Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  4. 58. Length of Last Word

    题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...

  5. Java [Leetcode 58]Length of Last Word

    题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...

  6. 58. Length of Last Word【leetcode】

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  7. leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法

    Length of Last Word  Given a string s consists of upper/lower-case alphabets and empty space charact ...

  8. 【LeetCode】58. Length of Last Word 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 双指针 单指针 日期 题目地址:https: ...

  9. 【LeetCode】58 - Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

随机推荐

  1. SpringCloud的Archaius - 动态管理属性配置

    参考链接:http://www.th7.cn/Program/java/201608/919853.shtml 一.Archaius是什么? Archaius用于动态管理属性配置文件. 参考自Gett ...

  2. uestc 1703一道更简单的字符串题目

    https://vjudge.net/problem/UESTC-1703 题意:略 思路: 枚举+字符串hash. ans从1到len开始枚举字符串的长度,然后就依次比较各段长度为ans的字符串的h ...

  3. 2018 6年iOS开发常用的三方库

    开发一般APP必备三方库,省力秘籍!!!本篇文章会经常更新最新常用的三方. 1.网络请求库 AFNetworking https://github.com/AFNetworking/AFNetwork ...

  4. html标记语言 --格式标记

    html标记语言 --格式标记 一.格式标记 1.<br>单标记,强制换行标记,让后面的文字.图片.表格等显示在下一行 2.<p>换段落标记 3.<center>居 ...

  5. webpack全局安装

    具体项目中才能使用webpack命令: npm install webpack -g npm install webpack-dev-server -g

  6. jacascript 判断元素尺寸和位置

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! getBoundingClientRect() 判断一个元素的尺寸和位置是简单的方法就是使用 obj.ge ...

  7. ubuntu安装eclipse

    官网下载界面 这里我选择的是Exlipse Oxygen的Eclipse IDE for Java EE Developers的64位版本. IBM直接下载地址 下载下来的是一个tar.gz的安装包, ...

  8. Windows下使用console线连接思科交换机

    在XP下可以直接使用内置工具"超级终端",在win7或者更高版本需要下载安装SecureCRT. 本文假设已经下载安装好了SecureCRT. 首先,将电脑连接console线.因 ...

  9. nodejs+express+mysql 增删改查(二)

    1.最早一篇关于express框架简单的增删改查文章,http://www.cnblogs.com/zhengyeye/p/nodejs.html#3947308:意外走红博客园,无奈自己之前一直没有 ...

  10. Java 静态内部类注意点

    静态内部类(嵌套类)的对象除了没有对生成它的外部类对象的引用特权外,与其他所有内部类完全一样. 在内部类不需要访问外部类时,应该使用静态内部类(嵌套类). 与常规内部类不同的是,静态内部类可以拥有静态 ...