58. Length of Last Word

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(string s) {
if(s.length() == )
{
return ;
}
int n = ;
int l = s.length();
for(int i = l-; i >= ; i--)
{
if(n == && s[i] == ' ')
{
continue;
}
if(s[i] != ' ')
{
n ++;
}
else
{
break;
}
}
return n;
}
};

最新文章

  1. [LeetCode] Minimum Window Substring 最小窗口子串
  2. Jsp 错题分析
  3. VC++ 获取当前模块的路径(dll/exe)
  4. 安装mysql-connector-python
  5. (21)odoo中的QWeb模板引擎
  6. ios 多任务学习笔记
  7. C#版二维码生成器
  8. verilog学习笔记(2)_一个小module及其tb
  9. 秋招已过,各大厂的面试题分享一波 附C++实现
  10. Python3+Selenium2完整的自动化测试实现之旅(六):Python单元测试模块Unittest运用
  11. JavaScript中创建对象的几种模式
  12. 遗传算法(GA)
  13. DD常用命令组合
  14. C#常用的命名规则汇总
  15. 流程图工具Visual Paradigm for UML
  16. 网络对抗技术作业一 P201421410029
  17. 关于Python类属性与实例属性的讨论
  18. Mina的服务器
  19. 深入理解JAVA虚拟机阅读笔记5——Java内存模型与线程
  20. 向SQL Server 现有表中添加新列并添加描述.

热门文章

  1. centos下安装mycat
  2. ylbtech-Unitity-cs:计算阶乘值
  3. Android SDK Manager 设置代理
  4. HttpRequestValidationException (0x80004005) 的三种可能的解决方法
  5. (medium)LeetCode 220.Contains Duplicate III
  6. Longest Increasing Subsequence(DP)
  7. JavaScript显示分页按钮
  8. 本地搭建SVN服务器 局域网
  9. Redis附加功能之Redis事务
  10. 001 The Hello World In Csharp