【058-Length of Last Word (最后一个单词的长度)】


【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】

原题

  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.

题目大意

  给定一个由大写和小写字母组和空格组成的字符串,返回字符串中的最后一个单词长度。

解题思路

  先从后找第一个字母的位置x,假设没有找到就返回0,假设找到,再找第一个空格的位记为y(y可能是-1。由于没有找到空格),返回结果x-y。

代码实现

算法实现类

public class Solution {
public int lengthOfLastWord(String s) { int index = s.length() - 1; // 从后面向前找第一个不是' '的字符
while (index >=0 && s.charAt(index) == ' ') {
index--;
} if (index < 0) {
return 0;
} int tmp = index; // 运行到以下说明存在最后一个单词 // 从后面向前找第一个是' '的字符
while (index >=0 && s.charAt(index) != ' ') {
index--;
} return tmp - index;
}
}

评測结果

  点击图片,鼠标不释放。拖动一段位置。释放后在新的窗体中查看完整图片。

特别说明

欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47164433

【LeetCode-面试算法经典-Java实现】【058-Length of Last Word (最后一个单词的长度)】的更多相关文章

  1. 058 Length of Last Word 最后一个单词的长度

    给定一个字符串, 包含大小写字母.空格 ' ',请返回其最后一个单词的长度.如果不存在最后一个单词,请返回 0 .注意事项:一个单词的界定是,由字母组成,但不包含任何的空格.案例:输入: " ...

  2. [Leetcode] Length of last word 最后一个单词的长度

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

  3. lintcode:Length of Last Word 最后一个单词的长度

    题目: 最后一个单词的长度 给定一个字符串, 包含大小写字母.空格' ',请返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 样例 给定 s = "Hello World& ...

  4. 58. Length of Last Word最后一个单词的长度

    [抄题]: [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: "b a " 最后一位是空格,可能误 ...

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

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

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

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

  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. [LintCode] Length of Last Word 求末尾单词的长度

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

  9. 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

    [139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...

  10. 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】

    [053-Maximum Subarray(最大子数组和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Find the contiguous subarray w ...

随机推荐

  1. IntelliJ IDEA 10 配置 Tomcat7

    http://blog.csdn.net/neutrojan/article/details/8064145

  2. 35.QT蝴蝶飞舞

    fly.h #ifndef FLY_H #define FLY_H #include <QObject> #include <QPainter> #include <QG ...

  3. 关于spring和extjs对接的过程简述

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...

  4. kettle的下载、安装和初步使用(Ubuntu 16.04平台下)(图文详解)

    不多说,直接上干货! 能够看我这篇博客的博友们,想必是已经具备一定基础了. 扩展博客 kettle的下载.安装和初步使用(windows平台下)(图文详解) kettle的下载 žKettle可以在h ...

  5. HD-ACM算法专攻系列(17)——find your present (2)

    题目描述: 源码: #include"iostream" #include"string" using namespace std; bool IsFirstH ...

  6. Android Studio 插件 GsonFormat :你还在烦恼 为 Json格式 生成 JavaBean实体类吗?

    在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...

  7. html5 好用功能总结

    1.表格元素 a.<caption>设置表格标题 b.<colgroup> . <col> 设置列 //style span 2.分组元素 a.<blockq ...

  8. 用latex画化学结构式

    最近写论文需要画化学结构式,于是想到用Latex里的包.但是一看知乎里面的大牛们一片口诛笔伐,说还是Chemdraw好.用latex是装... 不管怎么说,还是查了一下.首先需要下载chemfig.t ...

  9. Android中的事件分发机制

    Android中的事件分发机制 作者:丁明祥 邮箱:2780087178@qq.com 这篇文章这周之内尽量写完 参考资料: Android事件分发机制完全解析,带你从源码的角度彻底理解(上) And ...

  10. FrameLsyout

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_ ...