题目:

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. CSU - 2055 Wells‘s Lottery

    Description As is known to all, Wells is impoverished. When God heard that, God decide to help the p ...

  2. 三、redis系列之事务

    1. 绪言 Redis也提供了事务机制,可以一次执行多个命令,本质是一组命令的集合.一个事务中的所有命令都会序列化,按顺序地串行化执行而不会被其他命令插入,不许加塞.但Redis对事务的支持是部分支持 ...

  3. 洛谷——P1345 [USACO5.4]奶牛的电信Telecowmunication

    P1345 [USACO5.4]奶牛的电信Telecowmunication 题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮 ...

  4. 复杂密码生成工具apg

    复杂密码生成工具apg   密码是身份认证的重要方式.由于密码爆破方式的存在,弱密码非常不安全.为了构建复杂密码,Kali Linux预置了一个复杂密码生成工具apg.该工具可以提供可读密码和随机字符 ...

  5. redis 客户端命令

    Redis 通过监听一个 TCP 端口或者 Unix socket 的方式来接收来自客户端的连接 1 .CLIENT LIST 返回连接到 redis 服务的客户端列表 2 .CLIENT SETNA ...

  6. Codeforces Round #448 C. Square Subsets

    题目链接 Codeforces Round #448 C. Square Subsets 题解 质因数 *质因数 = 平方数,问题转化成求异或方程组解的个数 求出答案就是\(2^{自由元-1}\) , ...

  7. python 中__name__ = '__main__' 的作用,到底干嘛的?

    python 中__name__ = 'main' 的作用,到底干嘛的? 有句话经典的概括了这段代码的意义: "Make a script both importable and execu ...

  8. lnmp环境一键搭建及卸载

    系统需求: CentOS/Debian/Ubuntu Linux系统 需要2GB以上硬盘剩余空间 128M以上内存,OpenVZ的建议192MB以上(小内存请勿使用64位系统) VPS或服务器必须已经 ...

  9. BZOJ 2756: [SCOI2012]奇怪的游戏 网络流/二分

    2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 1594  Solved: 396[Submit][Stat ...

  10. 记一次centos7.2下用crontab执行定时任务的过程(初级)

    实验目的:每分钟往某个文件写数据(crontab最小单位是分钟),具体shell命令我是放在一个文件里的.先创建两个空文件:/tmp/a.txt(目标文件)和/tmp/a.sh(脚本文件). 命令如下 ...