【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. 2015 Multi-University Training Contest 2 1006 Friends 壮压

    题目链接 题意:t 组測试数据,每组測试数据有 n个人,m条关系 每条关系能够是 "线上关系" 或者 "线下关系". 要求每一个人的线上关系(条数) == 线下 ...

  2. Line(扩展欧几里得)

    题意:本题给出一个直线,推断是否有整数点在这条直线上: 分析:本题最重要的是在给出的直线是不是平行于坐标轴,即A是不是为0或B是不是为0..此外.本题另一点就是C输入之后要取其相反数,才干进行扩展欧几 ...

  3. iOS CoreImage图片处理动态渲染(滤镜)

    // //  ViewController.m //  CoreImageOfDong // //  Created by Dong on 15/6/30. //  Copyright (c) 201 ...

  4. Codeforces Round #316 (Div. 2)E. Pig and Palindromes DP

    E. Pig and Palindromes   Peppa the Pig was walking and walked into the forest. What a strange coinci ...

  5. 点击了一个link button,查看后台调用

    使用F12进行监视 本身是一个linkbutton,可以看到绑定了一个JavaScript <a id="gvStaticConnection_ctl02_fresh" hr ...

  6. php基础知识(一)--2017-04-14

    1.Php的两种打开方式: 第一种方式:地址栏打开:http://localhost/0414/qq.php     地址栏输入localhost/  就是phpstudy下的www文件夹 第二种:新 ...

  7. Spark RDD概念学习系列之不同角度看RDD

    不多说,直接上干货!

  8. Fildder 4接口测试工具Post请求方式

  9. ES6 | ES6新语法 在编码实践中的应用

    本章探讨如何将 ES6 的新语法,运用到编码实践之中,与传统的 JavaScript 语法结合在一起,写出合理的.易于阅读和维护的代码. 多家公司和组织已经公开了它们的风格规范,本文的内容主要参考了  ...

  10. Oracle自制事务

    数据库事务是一种单元操作,要么全部操作成功,要么全部失败.在Oracle中,一个事务是从执行第一个数据操作语言(DML)语句开始的,直到执行一个COMMIT语句,提交保存事务,或执行一个ROLLBAC ...