题目:

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. (Easy)

分析:

注意把最后的空格处理掉即可。

代码:

 class Solution {
public:
int lengthOfLastWord(string s) {
int start = s.size() - ;
int result = ;
while (s[start] == ' ') {
start--;
}
for (int i = start; i >= ; --i) {
if (s[i] != ' ') {
result++;
}
else {
return result;
}
}
return result;
}
};

LeetCode58 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. [Swift]LeetCode58. 最后一个单词的长度 | 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 the l ...

  4. 【leetcode】Length of Last Word

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

  5. [LeetCode] Length of Last Word

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

  6. [LintCode] Length of Last Word 求末尾单词的长度

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

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

  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. 【Length of Last Word】cpp

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

随机推荐

  1. git与github建立链接(学习笔记)

    总结步骤: 1.将所有文件添加到本库 git add . 2. git commit -m "提示信息随便写" 3.查看git修改状态 git status 4.获取远程库与本地同 ...

  2. VMWare 下 Ubuntu 18.04 的文件共享

    突然某天发现 /mnt/hgfs 下共享的文件夹没了... apt-get install open-vm-tools mkdir /mnt/hgfs vmhgfs-fuse .host:/ /mnt ...

  3. Spring松耦合示例(转)& IOC

    Spring松耦合示例 轻松学习Spring<一> IoC容器和Dependency Injection模式 最近公司需要,项目中要用到Spring和Ibatis.趁着过年好好学习学习.I ...

  4. Java Servlet实现下载文件

    一.配置servlet 在WebContent(以前的eclipse版本是WebRoot)文件夹下,有一个web.xml 修改web.xml ,加入以下代码 <servlet> <s ...

  5. 基于Kebernetes 构建.NET Core技术中台

    原文:基于Kebernetes 构建.NET Core技术中台 我们为什么需要中台 我们现在处于企业信息化的新时代.为什么这样说呢? 过去企业信息化的主流重心是企业内部信息化.但现在以及未来的企业信息 ...

  6. ubuntu设置终端命令历史记录

    ----------------------------------------------- HISTTIMEFORMAT='%F %T ' # 使用HISTTIMEFORMAT在历史中显示TIME ...

  7. OpenSmtp 发送邮件

    1.采用发送一个简单邮件 示例: private int smtpPort; private string smtpHost; private int recieveTimeout; private ...

  8. Django项目:CRM(客户关系管理系统)--16--08PerfectCRM实现King_admin显示注册表的字段表头

    # king_urls.py # ————————02PerfectCRM创建ADMIN页面———————— from django.conf.urls import url from king_ad ...

  9. CB Insights,201608月174家独角兽榜单出炉,上榜的33家中国公司都是谁?

    全球最新独角兽榜单出炉,上榜的33家中国公司都是谁? Monica  2016-09-15   近日,美国市场调研公司CB Insights发布了全球独角兽榜单(估值10亿美元以上),共有来自21个国 ...

  10. 简单介绍几个CSSReset的方法

    对于小型的网站来说,用这个并不会带来大的资源浪费,但如果是像Yahoo这种架构非常大的网站,必须要有选择地进行CSS重设,以减 少资源浪费. 正在使用CSS的你,用过CSS Reset吗?当然,或许你 ...