这个题目很简单,给一个字符串,然后返回最后一个单词的长度就行。题目如下:

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(const char *s)
{
string tmp = s;
reverse(tmp.begin(), tmp.end()); int sum, i, len;
sum = i = 0;
len = tmp.length();
while (i < len)
{
if (isalpha(tmp[i]))
{
sum++;
i++;
break;
}
else
{
i++;
}
} while (tmp[i] != ' ' && i < len)
{
sum++;
i++;
} return sum;
}
};

如上,不难。

[leetcode] 18. Length of Last Word的更多相关文章

  1. leetcode 题解: Length of Last Word

    leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...

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

  4. Java for LeetCode 058 Length of Last Word

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

  5. LeetCode 58. Length of Last Word

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

  6. Java [Leetcode 58]Length of Last Word

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

  7. LeetCode(48)-Length of Last Word

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

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

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

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

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

随机推荐

  1. C# split分割多个字符

    string[] myAgent = agentInfo.Split(new string[] { "$#$" }, StringSplitOptions.None);

  2. 执行js,通过js显示隐藏的输入框,或者给input赋值

    在测试过程中,有些输入框是隐藏的,如果直接对他进行赋值,会找不到这个输入框,从而导致脚本运行失败. 例如下面的这个密码输入框: 登录密码输入框分为两个input,下面的是提示的,上面的才是真正存下来的 ...

  3. Oracle CHAR,VARCHAR,VARCHAR2,nvarchar类型的区别与使用(转载)

    一 varchar,varchar2,nvarchar,nvarchar2 四个类型都属于变长字符类型, varchar和varchar2的区别在与后者把所有字符都占两字节,前者只对汉字和全角等字符占 ...

  4. 使用git提交代码到GitHub

    0.下载Git Bash,在Windows系统可以用Git Bash通过简单的命令将代码提交到GitHub1.打开项目所在的文件夹,右键,"Git Bash Here"2.初次使用 ...

  5. 数字与字符串之间的转换以及%f与%lf的输入输出用法区别

    1.C++字符串与C字符串的转换: (1)string --> char * string str("OK"); strcpy(p,str.c_str());//p是char ...

  6. SVN版本冲突问题

    --------------------siwuxie095 SVN 版本冲突问题 如:Jack 和 Mary 从仓库中将项目下载到本地,然后 Jack 修改了 项目中的一个文件,并上传到仓库中,之后 ...

  7. python的进程间的数据交互

    #先来看下如何实现多进程 # multiprocessing 这个是python的多进程的模块,我们会用到这个模块的很多方法 from multiprocessing import Process i ...

  8. log4j每天,每小时产生一日志文件

    log4j每天,每小时产生一日志文件 2016年08月05日 14:14:33 阅读数:6254 一.之前的文章中有log4j的相关配置以及属性的介绍,下面我们先把配置列出来:   log4j.roo ...

  9. Docker commit 制作weblogic镜像

    第一:前提条件 1.本机必须已经安装了docker 容器 2.pull 一个基础的镜像  如图:rastasheep/ubuntu-sshd 第二:利用docker commit  命令 将容器的状态 ...

  10. dede后台一片空白

    原因是你修改了后台的数据库连接信息文件data/common.inc.php,保存的后文件编码并不是utf-8,而是变成了ANSI或utf-8 + bom的. 解决方法: 用editplus或note ...