题目:

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.

题解:

这道题主要是考虑一下最后是不是空格,方法是倒着找不是空格的字符并计数,如果遇到空格且计数不是0,说明最后一个单词已经被计数了,所以可以返回了。

代码如下:

 1     public int lengthOfLastWord(String s) {
 2          if (s == null || s.length() == 0)  
 3             return 0;  
 4          
 5         int len = s.length();  
 6         int count = 0;  
 7         for (int i = len - 1; i >= 0; i--) {  
 8             if (s.charAt(i) != ' ') {  
 9                 count++;  
             }  
             if(s.charAt(i)==' '&&count != 0){  
                 return count;  
             }  
         }  
         return count;  
     }

当然这道题也能用投机取巧的方法,用split函数把字符串按照空格分隔好,返回最后那个就行。。。

代码如下:

     public int lengthOfLastWord(String s) {
         String[] a = s.split(" ");
         if(a == null || a.length == 0)
             return 0;
 
         return a[a.length-1].length();
     }

Length of Last Word leetocde java的更多相关文章

  1. Java for LeetCode 058 Length of Last Word

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

  2. Java [Leetcode 58]Length of Last Word

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

  3. 422. Length of Last Word【LintCode java】

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

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

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

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

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

  6. 58. Length of Last Word

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

  7. LeetCode——Length of Last Word

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

  8. LeetCode OJ:Length of Last Word(最后一个词的长度)

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

  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. PHP编程基础学习(一)——数据类型

    PHP一共支持8种原始类型,其中: 4种标量类型: boolean(布尔型) integer(整型) float/double(浮点型) string(字符串型) 两种复合类型: array(数组) ...

  2. ES6 一些笔记

    一. let/const: 1. “暂时性死区”概念:在代码块内,使用let/const命令声明变量之前,该变量都是不可用的.这在语法上,称为“暂时性死区”(temporal dead zone,简称 ...

  3. win10怎么修改svn的用户和密码

    win10怎么修改svn的用户和密码(一般为默认),其他的系统也差不多 方法/步骤 1.方法一: 1.双击我的电脑在c盘找到auth文件夹 C:\Users\系统帐户名\AppData\Roaming ...

  4. linux驱动之一语点破天机

    <const 关键字> 在嵌入式系开发中,const关键字就是“只读”的意思   <为什么要ARM需要进行C语言环境的初始化> 在汇编情况下,指令的跳转,保护现场需要保存的数据 ...

  5. django设置数据库事务,通过异常处理回滚事务

    1.setting.py配置文件,开启事务ATOMIC_REQUESTS DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql' ...

  6. Lua脚本

    应用场景: 游戏开发 独立应用脚本 Web 应用脚本 扩展和数据库插件如:MySQL Proxy 和 MySQL WorkBench 安全系统,如入侵检测系统 1. 在很多时候,我们可以将Lua直接嵌 ...

  7. Apache2.4使用require指令进行访问控制--允许或限制IP访问/通过User-Agent禁止不友好网络爬虫

    从Apache2.2.X到Apache2.4.X,在配置上稍微有点不同,需要特别注意.现在记录下关于访问控制的配置. 经过苦苦搜索,终于配置成功.参考了这篇文章:http://www.cnblogs. ...

  8. BZOJ 1854: [Scoi2010]游戏 并查集

    1854: [Scoi2010]游戏 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 2672  Solved: 958[Submit][Status][ ...

  9. ZOJ 3631 Watashi's BG DFS

    J - Watashi's BG Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Subm ...

  10. 事物的隔离级别与并发完美体现了cap理论(确保数据完整、安全、一致性,在此基础上实现高性能访问(鱼和熊掌不可兼得)

    事物的隔离级别与并发完美体现了cap理论(确保数据完整.安全.一致性,在此基础上实现高性能访问(鱼和熊掌不可兼得)