58. Length of Last Word

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) {
if(s.length() == )
{
return ;
}
int n = ;
int l = s.length();
for(int i = l-; i >= ; i--)
{
if(n == && s[i] == ' ')
{
continue;
}
if(s[i] != ' ')
{
n ++;
}
else
{
break;
}
}
return n;
}
};

leetcode 58的更多相关文章

  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 难度:0

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

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

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

  4. Leetcode(58)题解:Length of Last Word

    https://leetcode.com/problems/length-of-last-word/ 题目: Given a string s consists of upper/lower-case ...

  5. Java实现 LeetCode 58 最后一个单词的长度

    58. 最后一个单词的长度 给定一个仅包含大小写字母和空格 ' ' 的字符串 s,返回其最后一个单词的长度. 如果字符串从左向右滚动显示,那么最后一个单词就是最后出现的单词. 如果不存在最后一个单词, ...

  6. Leetcode 58 Length of Last Word 字符串

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

  7. LeetCode 58. Length of Last Word

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

  8. Java [Leetcode 58]Length of Last Word

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

  9. LeetCode 58 Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

随机推荐

  1. 错误记录 git pull

    在安装open-falcon的nodata组件,更新库的时候,git pull 报错: You are not currently on a branch, so I cannot use any'b ...

  2. A note to "On global motions of a compressible barotropic and selfgravitating gas with density-dependent viscosities"

    Ducomet, Bernard; Nečasová, Šárka; Vasseur, Alexis. On global motions of a compressible barotropic a ...

  3. haxm intelx86加速模拟器的安装

    http://blog.csdn.net/huang9012/article/details/18082601 如果安装了还出现 创建模拟器 选项 CPU/abi的时候还出现 no system im ...

  4. 新找到的一款字体 fantasque-sans-mono

    http://www.ipreferjim.com/2015/03/your-ides-font-matters-fantasque-sans-mono/

  5. Jetty提交数据时报java.lang.IllegalStateException: Form too large270468>200000问题解决

    今天在使用Eclipse的Jetty插件做为服务器提交富文本编辑中的数据时,报如下异常: 在\eclipse\plugins目录下,找到org.mortbay.jetty.server_6.1.23. ...

  6. ubuntu 14.10 安装 zabbix

    在ubuntu 14.10 上部署 zabbix 2.x 基本软件包安装 既然是ubuntu系统,当然要用好apt-get神器. 参考教程 URL:http://blog.csdn.net/cloud ...

  7. ArcGIS栅格数据的合并和剪切

    ArcGIS栅格数据的合并和剪切 1.  合并:ArcToolBox-- àDataManagement--à Raster--àRaster Dataset--à Mosaic 或 Mosaic t ...

  8. [SQL]一组数据中Name列相同值的最大Je与最小je的差

    declare @t table(name varchar(),qy varchar(),je int) insert into @t union all union all union all un ...

  9. java 抽象类和接口总结

    1.抽象类和抽象方法必须使用abstract 关键字来修饰 2.抽象类不能实例化 3.抽象方法是为实现的方法,它与空方法时两个完全不同的概念 4.abstract 不能喝private static ...

  10. 自定义View的基本流程

    1.明确需求,确定你想实现的效果2.确定是使用组合控件的形式还是全新自定义的形式,组合控件即使用多个系统控件来合成一个新控件,你比如titilebar,这种形式相对简单,参考:http://blog. ...