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.

Example:

Input: "Hello World"
Output: 5

题意:

最后一个词的长度

Solution1: Just like do the simulation of  s.trim().

code

 /*
Time: O(n).
Space: O(1).
*/
class Solution {
public int lengthOfLastWord(String s) {
// corner case
if (s == null || s.length() == 0) return 0;
int right = s.length() - 1;
int i = 0;
// 先把last word右边的空格扫清
while (right > 0 && s.charAt(right) == ' ') right--;
i = right;
// 定住right, 用i来扫出last word的长度
while (i >= 0 && s.charAt(i) != ' ') i--;
return right - i;
}
}

[leetcode]58. Length of Last Word最后一个词的长度的更多相关文章

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

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

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

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

  3. Leetcode 58 Length of Last Word 字符串

    找出最后一个词的长度 class Solution { public: int lengthOfLastWord(string s) { , l = , b = ; while((b = s.find ...

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

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

  5. LeetCode 58. Length of Last Word

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

  6. Java [Leetcode 58]Length of Last Word

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

  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. lintcode:Length of Last Word 最后一个单词的长度

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

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

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

随机推荐

  1. hasura graphql-engine 集成zombodb

    zombodb 是一个很不错的pg 扩展,可以方便的把es 与pg 集成起来,使用方便 ,目前尽管有一些docker 镜像 但是版本都比较老,所以基于centos7 做了一个新的docker 镜像,同 ...

  2. mysql 分组排序前n + 长表转宽表

    MySQL数据库优化的八种方式(经典必看) 建表 CREATE TABLE if not EXISTS `bb` ( `id` int not null primary key auto_increm ...

  3. Java高级特性 第1节 集合框架和泛型

    Java中,存储多个同类型的数据,可以用数组来实现,但数组有一些缺陷: 数组长度固定不变,布恩那个很好的适应元素数量动态变化的情况 可以通过数组.length获取数组长度,却无法直接获取数组中实际存储 ...

  4. 如何用原生js开发一个Chrome扩展程序

    原文地址:How to Build a Simple Chrome Extension in Vanilla JavaScript 开发一个Chrome扩展程序非常简单,只需要使用原生的js就可以完成 ...

  5. .gitlab-ci.yml 配置文件,知识点

    官方介绍:https://docs.gitlab.com/ee/ci/yaml/README.html 翻译: https://segmentfault.com/a/1190000010442764

  6. 【C++】atof()

    转自:https://blog.csdn.net/zhaoyl03/article/details/8176387 atof 是ascII to float的缩写,它将ascII字符串转换为相应的单精 ...

  7. JavaScript常见的代码精简

    1.&& callback && callback() 等价于: if(callback){ callback(); } 表达的意思: 先判断 callback 是不是 ...

  8. ado.net调用带参数的存储过程

  9. 知识点:spring 完全手册

    什么是spring spring是一个开源框架,为简化企业级开发而生,使用spring可以使简单的java bean 实现以前只有EJG才能实现的功能. Spring是一个轻量级的控制反转(IoC)和 ...

  10. Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication

    Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication Overview Galera Cluster 由 Coders ...