给定一个字符串, 包含大小写字母、空格 ' ',请返回其最后一个单词的长度。
如果不存在最后一个单词,请返回 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. listen 70

    Better Sidewalks Could Bring Improved Public Health Most of our serious illnesses and deaths in the ...

  2. (转)linux 打开文件数 too many open files 解决方法

    too many open files 出现这句提示的原因是程序打开的文件/socket连接数量超过系统设定值. 查看每个用户最大允许打开文件数量 ulimit -a fdipzone@ubuntu: ...

  3. ACM学习历程—HDU5396 Expression(递推 && 计数)

    Problem Description Teacher Mai has n numbers a1,a2,⋯,an and n−1 operators("+", "-&qu ...

  4. ACM学习历程——UVA442 Matrix Chain Multiplication(栈)

    Description   Matrix Chain Multiplication  Matrix Chain Multiplication  Suppose you have to evaluate ...

  5. 【运维程序】简单的命令控制器(支持定时命令执行、重复定时任务命令和进程管理,开发这个小程序主要是为了方便管理服务进程)【个人github项目】

    一.前言: command-controller 一个运维程序,简单的命令控制器(支持定时命令执行和重复定时命令,开发这个程序主要是为了方便管理服务进程) 本来是要用python做的,但是之前做ffm ...

  6. ubuntu_deb安装命令

    dpkg命令常用格式如下: sudo dpkg -I iptux.deb#查看iptux.deb软件包的详细信息,包括软件名称.版本以及大小等(其中-I等价于--info) sudo dpkg -c ...

  7. Nuget:RQCode

    ylbtech-Nuget:QRCode 1.返回顶部 1. https://www.nuget.org/packages?q=qrcode 2. 2.qrcode.js返回顶部 1. https:/ ...

  8. libvirtError: 无效参数:could not find capabilities for domaintype=kvm

    libvirtError: 无效参数:could not find capabilities for domaintype=kvm 编辑/etc/nova/nova.conf 在[libvirt] 添 ...

  9. Redux API之Store

    Store Store 就是用来维持应用所有的 state 树 的一个对象. 改变 store 内 state 的惟一途径是对它 dispatch 一个action. Store 不是类.它只是有几个 ...

  10. 《Java多线程编程核心技术》读后感(九)

    当interrupt方法遇到wait方法 当线程呈wait()状态时,调用线程对象的interrupt()会出现InterruptedException异常 package Third; public ...