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.

解题思路:

从后往前遍历,用一个指针rightIndex表示第一次出现飞空格的位置,然后继续遍历,找出第一次出现空格的位置即可,JAVA实现如下:

   static public int lengthOfLastWord(String s) {
int rightIndex=s.length()-1;
while(rightIndex>=0&&s.charAt(rightIndex)==' ')
rightIndex--;
if(rightIndex<0)
return 0;
for(int i=rightIndex-1;i>=0;i--)
if(s.charAt(i)==' ')
return rightIndex-i;
return rightIndex+1;
}

Java for LeetCode 058 Length of Last Word的更多相关文章

  1. Java [Leetcode 58]Length of Last Word

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

  2. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  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 题解: Length of Last Word

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

  5. 【leetcode】Length of Last Word

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

  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(48)-Length of Last Word

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

  8. [leetcode]58. Length of Last Word最后一个词的长度

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

  9. 【LeetCode】- Length of Last Word(最后一个单词的长度)

    [ 问题: ] Given a string s consists of upper/lower-case alphabets and empty space characters ' ', retu ...

随机推荐

  1. 【codevs1014/1068】背包型动态规划

    分析: 状态转移方程: v[j]=max(v[j],v[j-a[i]]+a[i]) (j ← tol downto a[i]) /* 作者:flipped 题目:p1014 装箱问题 */ #incl ...

  2. 一个项目中哪些文件是要上传到 git上的,哪些是不必要的

  3. [NOIP2009] 提高组 洛谷P1071 潜伏者

    题目描述 R 国和 S 国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动.历尽艰险后,潜伏于 S 国的 R 国间谍小 C 终于摸清了 S 国军用密码的编码规则: 1. S 国军方内部欲发送的原 ...

  4. TYVJ1305 最大子序和

    描述 输入一个长度为n的整数序列,从中找出一段不超过M的连续子序列,使得整个序列的和最大. 例如 1,-3,5,1,-2,3 当m=4时,S=5+1-2+3=7当m=2或m=3时,S=5+1=6 输入 ...

  5. Tarjan算法详解理解集合

    [功能] Tarjan算法的用途之一是,求一个有向图G=(V,E)里极大强连通分量.强连通分量是指有向图G里顶点间能互相到达的子图.而如果一个强连通分量已经没有被其它强通分量完全包含的话,那么这个强连 ...

  6. automapper初步

    首先引入 automapper.dll using System; using System.Collections.Generic; using System.Linq; using System. ...

  7. Eclipse下搭建Hadoop2.4.0开发环境

    一.安装Eclipse 下载Eclipse,解压安装,例如安装到/usr/local,即/usr/local/eclipse 4.3.1版本下载地址:http://pan.baidu.com/s/1e ...

  8. background总结

    1. background-position background-position的百分比属性规则是:图片本身(x%,y%)的那个点,与背景区域的(x%,y%)的那个点重合. 具体可参考: http ...

  9. 如何卸载lnmp

    转自:https://www.centos.bz/2011/07/howto-remove-lnmp/ killall nginx /etc/init.d/mysql stop killall mys ...

  10. ldconfig deferred processing now taking place

    在ubuntu下面安装软件,安装结束后,提示:ldconfig deferred processing now taking place 到网上查询了一下,大概意思是说:软件安装完了,是否要重启电脑.