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.

求最后一个非空单词的长度而已,没什么注意点,直接看代码,理解题目意思就可以了:

 class Solution {
public:
int lengthOfLastWord(string s) {
int len = s.length();
if(len == ) return ;
while(s[len - ] == ' ')
if(len - >= )
len--;
else
return ;
len--;
int lenCount = ;
while(s[len] != ' ' && len >= ){
lenCount++, len--;
}
return lenCount;
}
};

java版本的代码如下所示,倒过来读很容易了:

 public class Solution {
public int lengthOfLastWord(String s) {
int i = s.length() - 1;
while(i >= 0 && s.charAt(i) == ' ')
i--;
int end = i+1;
while(i >= 0 && s.charAt(i) != ' ')
i--;
int start = i+1;
return end - start;
}
}

LeetCode OJ: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] Length of last word 最后一个单词的长度

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

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

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

  5. 058 Length of Last Word 最后一个单词的长度

    给定一个字符串, 包含大小写字母.空格 ' ',请返回其最后一个单词的长度.如果不存在最后一个单词,请返回 0 .注意事项:一个单词的界定是,由字母组成,但不包含任何的空格.案例:输入: " ...

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

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

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

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

  8. Leetcode 58 Length of Last Word 字符串

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

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

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

随机推荐

  1. 2.6 基于ARDUINO UNO+MC20的路径显示功能

    需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...

  2. docker学习笔记1-- 用Toolbox安装Docker--介绍Docker Machine

    使用的是Docker Toolbox,非Docker for Windows 一.docker的认识与安装(windows安装) http://blog.csdn.net/tina_ttl/artic ...

  3. docker-compose no such image

    是由于docker-compose旧缓存的问题,执行docker-compose down即可,再重新up

  4. 常见Web源码泄露总结

    来自:http://www.hacksec.cn/Penetration-test/474.html 摘要 背景 本文主要是记录一下常见的源码泄漏问题,这些经常在web渗透测试以及CTF中出现. .h ...

  5. 【HackerRank】Maximizing XOR

    给定两个整数:L 和 R ∀ L ≤ A ≤ B ≤ R, 找出 A xor B 的最大值. 输入格式 第一行包含 L 第一行包含 R 数据范围 1 ≤ L ≤ R ≤ 103 输出格式 输出最大的异 ...

  6. imx6qsbd kpp

    转: https://blog.csdn.net/zyaiwmy/article/details/54313025 https://www.aliyun.com/jiaocheng/123973.ht ...

  7. node做验证码

    使用了ccap插件 1.安装: 通用方法:npm install ccap 2. cnst ccap= require('ccap')({ width: 128, height: 40, offset ...

  8. CSS3带小图标垂直下拉菜单

    在线演示 本地下载

  9. 4.2《深入理解计算机系统》笔记(五)并发、多进程和多线程【Final】

    该书中第11章是写web服务器的搭建,无奈对web还比较陌生.还没有搞明白. 这些所谓的并发,其实都是操作系统做的事情,比如,多进程是操作系统fork函数实现的.I/O多路复用需要内核挂起进程.多线程 ...

  10. Harbor 镜像管理专家

    Harbor是一个企业级的镜像管理仓库,是VMware主导的一个开源项目(github地址:https://github.com/vmware/harbor). Harbor提供了以下功能特性: Cl ...