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. ExtJs学习笔记之Button组件

    按钮Button组件 可以使用该组件的创建简单的按钮. 可以自定义属性包括 aligned icons, dropdown menus, tooltips 和 sizing options. 当出发点 ...

  2. java数组操作

    @Bizlet("数据对象扩展运算逻辑")public class DataObjectExt { private DataObjectExt(){ //工具类不能实例化 } /* ...

  3. [转] matlab saveas 和imwrite的区别

    http://hi.baidu.com/curbzz/item/04a69e805fc334e3e596e035 saveas(handle,['目录','文件名']) 如果只有一幅图,handle设 ...

  4. apply()和call()的区别

    这两个方法的用途都是在特定的作用域中调用函数,实际上等于设置函数体内this对象的值. apply()接收两个参数:一个参数是在其中运行的作用域,另一个是参数数组(可以是Array实例,也可以是arg ...

  5. LintCode "Longest Increasing Continuous subsequence II" !!

    DFS + Memorized Search (DP) class Solution { int dfs(int i, int j, int row, int col, vector<vecto ...

  6. 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^ ...

  7. FTP主/被动模式的原理

    ---------------------------------------------------------------------------------------------------- ...

  8. Erlang库 -- 有意思的库汇总

    抄自这里 首先,库存在的目的大致可分为:1.提供便利2.尽可能解决一些痛点 首先,我们先明确一下Erlang编程语言的一些痛点(伪痛点):1,单进程问题Erlang虚拟机属于抢占式调度,抢占式调度有很 ...

  9. ubuntu ipv6网络电视(avplay)

    首先在ubuntu下安装好ipv6 (话说是已经装好了的,不过最好检查以下) 网上有很多资源,我不写了. 测试一下 :ping ipv6.scau.edu.cn 另外,关于ipv6 网络播放器很多人推 ...

  10. Mysql游标的简明写法

    -- cursor 游标/*declare 声明; declare 游标名 cursor for select_statement;open 找开; open 游标名fetch 取值; fetch 游 ...