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