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

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. fiddler 抓取 逍遥安卓模拟器 https包

    1.打开fiddler,进行相关设置 Tools--Fiddler Options 接下来进行客户端网络配置 1 查看电脑ip地址,ipconfig 逍遥游模拟器中使用自带的浏览器,访问192.168 ...

  2. jsp 不显示换行 Eclipse复制一行快捷键

    jsp通过out.println();不能换行.html中需要标签<br/>才可以 Eclipse 复制整行代码移动:Ctrl+Alt+↑/↓.但是跟系统屏幕上下切换冲突,可以选择,在电脑 ...

  3. What is API Level?

    [What is API Level?] 参考:http://android.xsoftlab.net/guide/topics/manifest/uses-sdk-element.html#ApiL ...

  4. jquery 显示 隐藏

    参考 http://www.w3school.com.cn/jquery/jquery_hide_show.asp $("#a").hide(); $("#a" ...

  5. PHP的zip、unzip类详解

    1.打开一个ZIP包,用于读取.写入或修改 open(string $filename [, int $flags]) $filename - 文件名 $flags - 打开模式 ZIPARCHIVE ...

  6. Java注解(Annotation)用法:利用注解和反射机制指定列名导出数据库数据

    闲来没事,想了一个应用的例子:用java如何把数据库的数据根据我们指定的某几列,如第2列,第4列,第6列导出来到Excel里? 写代码也是为了应用的,写好的代码更重要的是在于思考.我自己思考了这个示例 ...

  7. Broadcast总结 service

    有时候离开应用就会接收不到系统的广播是因为系统默认发送的广播都会有一个参数 ntent startIntent = new Intent();startIntent.putExtra("pk ...

  8. CentOS7下搭建基本LNMP环境,部署WordPress

    系统环境:CentOS Linux release 7.4.1708 (Core) 3.10.0-693.el7.x86_64 软件版本:nginx-1.12.2.tar.gz php 7.1.11 ...

  9. mysql rc模式时binlog_format=row的解释【转】

    总体来说:在 tx_isolation= READ-COMMITTED .binlog_format =statement 的情况下,mysql 没有gap 锁,这样binlog 记录的数据修改的顺序 ...

  10. PAT 1003 我要通过!(20)(代码+思路)

    1003 我要通过!(20)(20 分)提问 "答案正确"是自动判题系统给出的最令人欢喜的回复.本题属于PAT的"答案正确"大派送 -- 只要读入的字符串满足下 ...