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

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. sudoers的权限被改,又忘记了root密码,又不能重启。这么做。

    报下面这个错 sudo: /etc/sudoers is world writablesudo: no valid sudoers sources found, quittingsudo: unabl ...

  2. dom node 查找父级parentNode

    var o = document.querySelectorAll("a[href='baidu.com']"); var p = o[o.length-1];console.lo ...

  3. maven 项目 编码

    今天在DOS下执行mvn compile命令时报错说缺少必要符号,事实上根本就没有缺少,但何以如此呢,为啥eclipse在编译时就没有这问题呢? 原因是编码的问题造成的! eclipse在编译的使用使 ...

  4. c++ stl常用

    #include<iostream>#include<string>#include<vector>#include<list>#include< ...

  5. dede的cfg_keywords和cfg_description无法显示

    问题:在生成html文件时,网页的keywords和description的content为空,但后台显示这两项是有值的.   解决方案: 1.设置 系统->系统基本参数->站点根网址 设 ...

  6. IIS支持IPA、APK文件的下载

    IIS里MIME类型中默认是没有ipa,apk文件的,所以无法直接通过网络下载.   解决方法如下: 1.打开IIS信息服务管理器,选中自已的网站,在右边面板中找到MIME类型. 2.双击打开,点击右 ...

  7. springmvc在处理请求过程中出现异常信息交由异常处理器进行处理,自定义异常处理器可以实现一个系统的异常处理逻辑。为了区别不同的异常通常根据异常类型自定义异常类,这里我们创建一个自定义系统异常,如果controller、service、dao抛出此类异常说明是系统预期处理的异常信息。

    springmvc在处理请求过程中出现异常信息交由异常处理器进行处理,自定义异常处理器可以实现一个系统的异常处理逻辑. 1.1 异常处理思路 系统中异常包括两类:预期异常和运行时异常RuntimeEx ...

  8. Castle ActiveRecord学习(五)使用HQL语句查询

    来源:http://www.cnblogs.com/Terrylee/archive/2006/04/12/372823.html 一.HQL简单介绍HQL全名是Hibernate Query Lan ...

  9. 源代码安装grub-customizer

    wget https://launchpad.net/grub-customizer/5.0/5.0.6/+download/grub-customizer_5.0.6.tar.gztar zxvf ...

  10. beego启动找不到conf的原因

    beego配置文件路径如下: app.conf内容 httpaddr = "192.168.199.178" httpport = appname = SecProxy runmo ...