给定一个字符串, 包含大小写字母、空格 ' ',请返回其最后一个单词的长度。
如果不存在最后一个单词,请返回 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. Java微信公众平台开发_04_自定义菜单

    一.本节要点 1.菜单相关实体类的封装 参考官方文档中的请求包的内容,对菜单相关实体类进行封装. 2.数据传输格式—JSON 自定义菜单中请求包的数据是Json字符串格式的,请参见:  Java_数据 ...

  2. 如何通过giihub下载软件

    因为不懂英文, 所以找到了网站也不知道要怎么下载? 需求: 假设要下载的的一个jar包,  mybatis-generator 1.  利用搜索引擎 2. 点进去, 看到那个release  (rel ...

  3. css实现下拉列表

    像上面的要想实现 Hover 标题时  内容区下拉的效果,一般是要用js实现: 先获取内容区的高度,由于内容区刚开始可能是隐藏的,那么怎么才能获取其高度呢?方法是先给其元素设置绝对定位并把位置保持和之 ...

  4. CodeForces - 592D: Super M(虚树+树的直径)

    Ari the monster is not an ordinary monster. She is the hidden identity of Super M, the Byteforces’ s ...

  5. ACM学习历程——HDU3333 Turing Tree(线段树 && 离线操作)

    Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems abou ...

  6. lwip【6】LWIP使用经验

    LWIP使用经验 一 LWIP内存管理 LWIP的内存管理使用了2种方式:内存池memp和内存堆mem,如图1所示. 内存池的特点是预先开辟多组固定大小的内存块组织成链表,实现简单,分配和回收速度快, ...

  7. 使用Axis2方式发布webService的三种方式

    1.Axis2的下载和安装 首先可以下载如下两个zip包:axis2-1.6.1-bin.zipaxis2-1.6.1-war.zip其中 axis2-1.6.1-bin.zip文件中包含了Axis2 ...

  8. 2-5 Flutter开发环境与Android开发环境设置详解(Windows)

    第二个是国内服务器的网址 andoid stuido的一些使用的说明文档 https://developer.android.google.cn/studio/intro 安装Flutter Dart ...

  9. 图解SynchronousQueue原理详解-非公平模式

    SynchronousQueue原理详解-非公平模式 开篇 说明:本文分析采用的是jdk1.8 约定:下面内容中Ref-xxx代表的是引用地址,引用对应的节点 前面已经讲解了公平模式的内容,今天来讲解 ...

  10. 基于unity3d游戏的android版本逆向初探

    https://bbs.pediy.com/thread-212532.htm [文章标题]: 基于unity3d游戏的android版本逆向初探 [文章作者]: dreaman [作者邮箱]: [e ...