Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word (last word means the last appearing word if we loop from left to right) in the string.

If the last word does not exist, return 0.

Note: A word is defined as a maximal substring consisting of non-space characters only.

Example:

Input: "Hello World"
Output: 5
class Solution {
public int lengthOfLastWord(String s) {
if (s == null || s.length() == 0) {
return 0;
}
String[] strArr = s.split("\\s+");
// for case " "
return strArr.length == 0 ? 0: strArr[strArr.length - 1].length();
}
}

[LC] 58. Length of Last Word的更多相关文章

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

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

  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. LeetCode 58. Length of Last Word

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

  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. 58. Length of Last Word

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

  7. Java [Leetcode 58]Length of Last Word

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

  8. 58. Length of Last Word【leetcode】

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

  9. <LeetCode OJ> 58. Length of Last Word

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

随机推荐

  1. 一个算法题--Self Crossing

    You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to th ...

  2. nodejs(6)express学习

    1.简单认识express express::一个快速的网站开发框架,封装了原生的http模块,用起来更方便:API更人性化 特点 基于Node.js平台之上,进一步封装了 http 模块,从而提供了 ...

  3. CSU_1414 Query On a Tree BFS序+DFS时间戳进行预处理

    2014 csu校赛 I 题,比赛的时候拿着他看了几个小时愣是没弄出好的方法,我们也想过统计出每个root的节点总数,然后减去离它d层的子节点的数目,即为答案.但是因为树的存储是无序的,所以每次为了找 ...

  4. github访问过慢解决

    为了更加愉快地使用全球最大同性交友网站上的优质资源,我们来做一些简单的本机上的调整. 通过查看下载链接,能够发现最终被指向到Amazon的服务器(http://github-cloud.s3.amaz ...

  5. Python语言基础与应用 (P16)上机练习:基本数据类型

    本文是笔者在学习MOOC课程<Python语言基础与应用> (北京大学-陈斌)中根据上机课时的要求写下在代码 课程总链接: 中国大学MOOC B站 本节课链接 数值基本运算: 33和7+, ...

  6. [转帖西部数据的Zonefs将会登陆Linux 5.6内核]

    西部数据的Zonefs将会登陆Linux 5.6内核 https://www.cnbeta.com/articles/tech/948875.htm 据说SMR 能够提高25%的存储密度 但是会造成严 ...

  7. AttributeError: module 'selenium.webdriver.common.service' has no attribute 'Service'

    今天爬虫时需要使用到selenium, 使用pip install selenium进行安装. 可是一开始写程序就遇到了AttributeError: module 'selenium.webdriv ...

  8. 201604-2 俄罗斯方块 Java

    大家谁能帮我看看是哪里不对,提交到系统中是0分,在Eclipse中可以得出例子中的结果 思路: 题目中有两个关键点:如何模拟下落的过程,如何判断方块下落在哪里停止. 在数据的存储上,需要保存整个&qu ...

  9. python图像处理常用方法

    在线标注网站 https://gitlab.com/vgg/via http://www.robots.ox.ac.uk/~vgg/software/via/via.html 数组与图像互转 from ...

  10. 17.3.15---关于GPIO控制流水灯的信息

    添加一个网址: http://rmingwang.com/gpio-control-flow-lamp-code-archive.html 还有一个 http://www.openedv.com/po ...