给定一个字符串, 包含大小写字母、空格 ' ',请返回其最后一个单词的长度。
如果不存在最后一个单词,请返回 0 。
注意事项:一个单词的界定是,由字母组成,但不包含任何的空格。
案例:
输入: "Hello World"
输出: 5
详见:https://leetcode.com/problems/length-of-last-word/description/

Java实现:

class Solution {
public int lengthOfLastWord(String s) {
int n=s.length();
if(n==0||s.isEmpty()){
return 0;
}
int cnt=0;
int right=n-1;
while(right>=0&&s.charAt(right)==' '){
--right;
}
while(right>=0&&s.charAt(right)!=' '){
--right;
++cnt;
}
return cnt;
}
}

058 Length of Last Word 最后一个单词的长度的更多相关文章

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

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

  2. [Leetcode] Length of last word 最后一个单词的长度

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

  3. 58. Length of Last Word最后一个单词的长度

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

  4. [LeetCode] 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. [leetcode]58. Length of Last Word最后一个词的长度

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

  7. [LintCode] Length of Last Word 求末尾单词的长度

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

  8. [Swift]LeetCode58. 最后一个单词的长度 | 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. Log4Net的使用之winform

    当我们将asp程序部署到远程服务器上的时候,如果遇到程序错误,如何能快速的判断我们程序的错误呢.所以-->Log4Net作为记录日志的一大神器,不得不学会熟练使用啊!没有那么多的原理,照猫画虎的 ...

  2. listen 75

    Hot Jupiters Smarten Search For Other Earths Scientists are looking for Earth like planets around ot ...

  3. CodeForces - 1000E :We Need More Bosses(无向图缩点+树的直径)

    Your friend is developing a computer game. He has already decided how the game world should look lik ...

  4. bootstrap框架日期时间 开始日期和结束日期选择

    页面表单查询时,常要求要查询一个日期时间段内的数据,若采用bootstrap框架的datetimepicker插件来控制,需要了解怎么个用法:

  5. Spring MVC 中/和/*的区别

    <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" ...

  6. ACM学习历程——POJ3295 Tautology(搜索,二叉树)

    Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some ...

  7. Set_ML

    参考资料:斯坦福(http://cs231n.github.io/linear-classify/:http://cs231n.stanford.edu/slides/2017/) Mastering ...

  8. Ubuntu——查看内存和CPU情况

    查看内存及cpu使用情况的命令:top 也可以安装htop工具,这样更直观,安装命令如下:sudo apt-get install htop安装完后,直接输入命令:htop

  9. .NETFramework:WebClient

    ylbtech-.NETFramework:WebClient 1.程序集 System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5 ...

  10. python2 + selenium + eclipse 中,通过django生产数据库表的时候报错

    python2 + selenium + eclipse 中,通过django生产数据库表的时候报错 解决: 1.查看自己电脑中,“开始-->控制面板-->管理工具-->服务--&g ...