题目:

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;
}
};

最新文章

  1. mysql中的优化, 简单的说了一下垂直分表, 水平分表(有几种模运算),读写分离.
  2. 使用vs中的发布功能发布asp.net core项目时遇到ERROR_CERTIFICATE_VALIDATION_FAILED错误
  3. bookstores网上书店测试缺陷报告1
  4. 【BZOJ1208】宠物收养所(平衡树,splay)
  5. p命名空间的使用(不推荐用)
  6. information_schema系列之字符集校验(CHARACTER_SETS,COLLATIONS,COLLATION_CHARACTER_SET_APPLICABILITY)
  7. Mac OS X 安装ruby环境
  8. C# 使用XML序列化对象(一)
  9. iOS代码规范文档
  10. CSS各种选择器
  11. json对象转字符串与json字符串转对象
  12. VC6.0建立控制台程序实现PDA应用
  13. CSDN-Markdown语法集锦
  14. Core Bluetooth【官方文档翻译】【02】
  15. Websense一面、二面及Offer
  16. JSP内置对象--4种属性范围 (pageContext,request,session,application)
  17. Thinking in scala (7)---- f(n)=f(n-1)+2f(n-2)+3f(n-3)
  18. 初识php soap 学习过程中的摘抄,便于后期翻阅
  19. LeetCode & Q13-Roman to Integer-Easy
  20. python常用的内置函数哈哈

热门文章

  1. Jeecms网站直接访问html静态页面
  2. Python实例 分割路径和文件名
  3. pstree进程管理
  4. linux常用目录介绍
  5. Android——系统权限
  6. MySQL学习-- UNION与UNION ALL
  7. DirectX11笔记(六)--Direct3D渲染2--VERTEX BUFFER
  8. 洛谷P1541 乌龟棋 [2010NOIP提高组]
  9. settings.gradle与build.gradle有什么区别
  10. 阿里云MVP:开发者的超能力,用技术创造更好世界