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.

这个题目只要注意如果字符串以‘ ’结尾的情况就行了例如"Hello World    "的返回值也是5

class Solution {
public:
int lengthOfLastWord(string s) {
size_t length=;
bool tag=false;
for(int i=s.size()-;i>=;--i)
{
if(s[i]!=' ')
{
tag=true;
++length;
}
if(s[i]==' '&&tag)return length;
}
return length;
}
};

LeetCode 58. 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. Java [Leetcode 58]Length of Last Word

    题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...

  3. [leetcode]58. Length of Last Word最后一个词的长度

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

  4. leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法

    Length of Last Word  Given a string s consists of upper/lower-case alphabets and empty space charact ...

  5. LeetCode: 58. Length of Last Word(Easy)

    1. 原题链接 https://leetcode.com/problems/length-of-last-word/description/ 2. 题目要求 给定一个String类型的字符串,字符串中 ...

  6. Leetcode 58 Length of Last Word 难度:0

    https://leetcode.com/problems/length-of-last-word/ int lengthOfLastWord(char* s) { int ans = 0; int ...

  7. Leetcode 58 Length of Last Word 字符串

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

  8. LeetCode练题——58. Length of Last Word

    1.题目 58. Length of Last Word——Easy Given a string s consists of upper/lower-case alphabets and empty ...

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

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

随机推荐

  1. Html下拉框的定义以及JS、Jquary取值、添加和移除

    现在有一id=test的下拉框,怎么拿到选中的那个值呢? 分别使用javascript原生的方法和jquery方法 <select id="test"  name=" ...

  2. 使用ExpandableListView——当有Group选项展开时,如何正确获取长按的Group选项。

    当我们使用ExpandableListView时,实现点击一个GroupView则展开ChidView,那么这个时候,Adapter的大小前后是有变化的. 例如:假设有20个GroupView,每个G ...

  3. Android monkey介绍

    Android monkey介绍 原文地址 1 简略 monkey是android下自动化测试比较重要的的一个工具,该工具可以运行在host端或者设备(模拟器或真实设备).它会向系统发送随机事件流(即 ...

  4. 51nod1258 序列求和V4

    T(n) = n^k,S(n) = T(1) + T(2) + ...... T(n).给出n和k,求S(n).   例如k = 2,n = 5,S(n) = 1^2 + 2^2 + 3^2 + 4^ ...

  5. C基础--函数指针的使用

    之前在看代码的时候,看了函数指针的使用,大体分为如下几类: 做一个function list,通过指针索引调用,使得处理功能类似的函数看起来更加清晰: 函数指针作为另一个函数的参数,用作回调: lin ...

  6. 访问修饰符internal

    internal(C# 参考) internal 关键字是类型和类型的成员 访问修饰符. 只有在同一程序集的文件中,内部类型或成员才是可访问的,如下例所示: public class BaseClas ...

  7. 《黄聪:手机移动站SEO优化教程》1、为什么要做手机移动端网站

    视频地址:http://v.youku.com/v_show/id_XNzE2Mzk3MjI4.html

  8. 杀死future处理的阻塞线程

    public class Test{ public static void main(String[] args){ ExecutorService pool = Executors.newFixed ...

  9. UVA 272 TEX Quotes

    TEX Quotes 题意: 变引号. 题解: 要想进步,真的要看一本好书,紫书P45 代码: #include<stdio.h> int main() { int c,q=1; whil ...

  10. winform属性

    WinForm为客户端程序必须在.NET Framework框架上运行 一.常用属性: 布局: AutoScroll:当控件内容超出可见区域是否显示滚动条: Autosize:当控件内容有超出时是否自 ...