leetcode 58
2024-08-29 11:48:51
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;
}
};
最新文章
- [LeetCode] Minimum Window Substring 最小窗口子串
- Jsp 错题分析
- VC++ 获取当前模块的路径(dll/exe)
- 安装mysql-connector-python
- (21)odoo中的QWeb模板引擎
- ios 多任务学习笔记
- C#版二维码生成器
- verilog学习笔记(2)_一个小module及其tb
- 秋招已过,各大厂的面试题分享一波 附C++实现
- Python3+Selenium2完整的自动化测试实现之旅(六):Python单元测试模块Unittest运用
- JavaScript中创建对象的几种模式
- 遗传算法(GA)
- DD常用命令组合
- C#常用的命名规则汇总
- 流程图工具Visual Paradigm for UML
- 网络对抗技术作业一 P201421410029
- 关于Python类属性与实例属性的讨论
- Mina的服务器
- 深入理解JAVA虚拟机阅读笔记5——Java内存模型与线程
- 向SQL Server 现有表中添加新列并添加描述.
热门文章
- centos下安装mycat
- ylbtech-Unitity-cs:计算阶乘值
- Android SDK Manager 设置代理
- HttpRequestValidationException (0x80004005) 的三种可能的解决方法
- (medium)LeetCode 220.Contains Duplicate III
- Longest Increasing Subsequence(DP)
- JavaScript显示分页按钮
- 本地搭建SVN服务器 局域网
- Redis附加功能之Redis事务
- 001 The Hello World In Csharp