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 n=s.size()-;
while(n>=&&s.at(n)&&s.at(n)==' ')n--;
int i=; for(;n>=&&s.at(n)&&s.at(n)!=' ';n--,i++);
return i;
}
};

【LeetCode】58 - Length of Last Word的更多相关文章

  1. 【LeetCode】58. Length of Last Word 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 双指针 单指针 日期 题目地址:https: ...

  2. 【一天一道LeetCode】#58. Length of Last Word

    一天一道LeetCode系列 (一)题目 Given a string s consists of upper/lower-case alphabets and empty space charact ...

  3. 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)

    [LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...

  4. 【leetcode❤python】 58. Length of Last Word

    #-*- coding: UTF-8 -*-#利用strip函数去掉字符串去除空格(其实是去除两边[左边和右边]空格)#利用split分离字符串成列表class Solution(object):   ...

  5. 【LeetCode】318. Maximum Product of Word Lengths 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 位运算 日期 题目地址:https://le ...

  6. 【LEETCODE】58、数组分类,适中级别,题目:238、78、287

    package y2019.Algorithm.array.medium; import java.util.Arrays; /** * @ProjectName: cutter-point * @P ...

  7. 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...

  8. 【LeetCode】211. Add and Search Word - Data structure design

    Add and Search Word - Data structure design Design a data structure that supports the following two ...

  9. 【LeetCode】58.最后一个单词的长度

    最后一个单词的长度 给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 说明:一个单词是指由字母组成,但不包含任何空格的字符串. 示例 ...

随机推荐

  1. php监听客户端连接状态

    http://bbs.csdn.net/topics/390661022 http://www.poluoluo.com/jzxy/201207/169977.html http://zhidao.b ...

  2. javascript 递归调用

    上源码:underscore.js var flatten = function(input, shallow, output) { if (shallow && _.every(in ...

  3. poj-3255-Roadblocks-路径可重复次短路

    题目: Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7075 Accepted: 2629 Descri ...

  4. 对Java不能多继承,只能单继承,却可以实现多个接口的理解

    1.java与C++的不同点在于多继承. Java:不能多继承,只能单继承,但可以实现多个接口 C++:可以实现多继承.例如: class A extends B implements C,D,E { ...

  5. 《OD学Oozie》20160807Oozie

    一.引入 MapReduce Job Hive 脚本任务 同一个业务:先后.定时调度 工作流: 定义工作流程 activity jbpm oozie: 大数据工作流定义与调度框架 专门定义与调度Map ...

  6. 转:最值得学习阅读的10个C语言开源项目代码

    阅读优秀代码是提高开发人员修为的一种捷径……  1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试 ...

  7. 8皇后以及N皇后算法探究,回溯算法的JAVA实现,递归方案

    八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例.该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出:在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同 ...

  8. content management system

    Defination of CMS: The definition of a CMS is an application (more likely web-based), that provides ...

  9. HDU 2084 数塔

    没什么好说的,这是我学DP的第一道题目. //#define LOCAL #include <iostream> #include <cstdio> #include < ...

  10. I.MX6 uSDHC SD card register

    /**************************************************************************** * I.MX6 uSDHC SD card ...