题目描述:

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.

解题思路:

先定位到字符串从后往前数第一个不为空字符的位置,然后进行判断。

从第一个位置向后读取,每遇到空字符则将计数器清零,然后下次继续计数。

代码如下:

public class Solution {
public int lengthOfLastWord(String s) {
int curLen = 0, len = 0, end = -1; if(s == null || (len = s.length()) == 0)
return 0;
for(int i = len - 1; i >= 0; i--){
if(s.charAt(i) != ' ') {
end = i;
break;
}
} for(int i = 0; i <= end; i++){
if(s.charAt(i) != ' ')
curLen++;
else {
curLen = 0;
}
}
return curLen;
}
}

  

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

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

  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 (最后单词的长度) 解题思路和方法

    Length of Last Word  Given a string s consists of upper/lower-case alphabets and empty space charact ...

  5. LeetCode: 58. Length of Last Word(Easy)

    1. 原题链接 https://leetcode.com/problems/length-of-last-word/description/ 2. 题目要求 给定一个String类型的字符串,字符串中 ...

  6. Leetcode 58 Length of Last Word 难度:0

    https://leetcode.com/problems/length-of-last-word/ int lengthOfLastWord(char* s) { int ans = 0; int ...

  7. Leetcode 58 Length of Last Word 字符串

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

  8. 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 ...

  9. 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 ...

随机推荐

  1. 【BZOJ】【1076】【SCOI2008】奖励关

    状压DP+数学期望 蒟蒻不会啊……看题跑…… Orz了一下Hzwer,发现自己现在真是太水了,难道不看题解就一道题也不会捉了吗? 题目数据范围不大……100*(2^16)很容易就跑过去了…… DP的时 ...

  2. uva 10313

    递推   参考了别人的解法 dp[i][j] 表示价值为i用j个硬币可以有多少种方法 dp[j][k] += dp[j-i][k-1] 意思是多加一枚价值为i的硬币,加上价值为j-i用k-1个硬币的总 ...

  3. CentOS(RHEL) 操作备忘

    1.安装中文语言包及切换 yum groupinstall chinese-support vi /etc/sysconfig/i18n change en_US to zh_CN 2.用户自动登录 ...

  4. 仪表盘 hostmap 新玩法让运维工作越玩越 high

    Cloud Insight 第13次新品发布会现在开始,首先非常感谢大家前来看我们的新功能发布会,下面我先给大家介绍一下新功能,之后有什么问题大家尽管问

  5. C# 常用的工具类

    编码解码.图片与byte[].日志文件写入和读取 /// <summary> /// 将字符串编码为Base64字符串 /// </summary> /// <param ...

  6. php获取数组长度的方法(有实例)

    php获取数组长度的方法,php为我们提供了两个函数可以计算一维数组长度,如count,sizeof都可以直接统计数组长度,还有获取二维数组的方法. 在php中获取数组长度方法很简单,php为我们提供 ...

  7. [杂题]URAL2047. Maths

    题意:构造一个长度为n的串,使得 除了第一个以外,每个位置的前缀和的因子个数恰好等于该位置上的数. n$\le 100000$ 举个例子$a_i$:2   4    6     6    4    8 ...

  8. Android中自定义Checkbox

    custom_checkbox.xml文件: <?xml version="1.0" encoding="utf-8"?> <selector ...

  9. linux内核--进程与线程

    http://blog.csdn.net/yusiguyuan/article/details/12154823 在<linux内核设计与实现>中第三章讲解了进程管理,在关于进程和线程的概 ...

  10. 如何在服务(Service)程序中显示对话框

    原文:http://www.vckbase.com/index.php/wv/94 服务程序(Service)一般是不能和用户进行交互的,所以他一般是不能显示窗口的.要和用户进行交互(如显示窗口),我 ...