[LeetCode] 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.
Example:
Input: "Hello World"
Output: 5
这道题难度不是很大。先对输入字符串做预处理,去掉开头和结尾的空格,然后用一个计数器来累计非空格的字符串的长度,遇到空格则将计数器清零,参见代码如下:
解法一:
class Solution {
public:
int lengthOfLastWord(string s) {
int left = , right = (int)s.size() - , res = ;
while (s[left] == ' ') ++left;
while (s[right] == ' ') --right;
for (int i = left; i <= right; ++i) {
if (s[i] == ' ') res = ;
else ++res;
}
return res;
}
};
昨晚睡觉前又想到了一种解法,其实不用上面那么复杂的,这里关心的主要是非空格的字符,那么实际上在遍历字符串的时候,如果遇到非空格的字符,只需要判断其前面一个位置的字符是否为空格,如果是的话,那么当前肯定是一个新词的开始,将计数器重置为1,如果不是的话,说明正在统计一个词的长度,计数器自增1即可。但是需要注意的是,当 i=0 的时候,无法访问前一个字符,所以这种情况要特别判断一下,归为计数器自增1那类,参见代码如下:
解法二:
class Solution {
public:
int lengthOfLastWord(string s) {
int res = ;
for (int i = ; i < s.size(); ++i) {
if (s[i] != ' ') {
if (i != && s[i - ] == ' ') res = ;
else ++res;
}
}
return res;
}
};
下面这种方法是第一种解法的优化版本,由于只关于最后一个单词的长度,所以开头有多少个空格起始并不需要在意,从字符串末尾开始,先将末尾的空格都去掉,然后开始找非空格的字符的长度即可,参见代码如下:
解法三:
class Solution {
public:
int lengthOfLastWord(string s) {
int right = s.size() - , res = ;
while (right >= && s[right] == ' ') --right;
while (right >= && s[right] != ' ' ) {
--right;
++res;
}
return res;
}
};
这道题用Java来做可以一行搞定,请参见这个帖子.
Github 同步地址:
https://github.com/grandyang/leetcode/issues/58
参考资料:
https://leetcode.com/problems/length-of-last-word/
https://leetcode.com/problems/length-of-last-word/discuss/21927/My-3-line-0-ms-java-solution
https://leetcode.com/problems/length-of-last-word/discuss/21892/7-lines-4ms-C%2B%2B-Solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 58. Length of Last Word 求末尾单词的长度的更多相关文章
- [LeetCode] Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [LintCode] Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [leetcode]58. Length of Last Word最后一个词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 58. Length of Last Word最后一个单词的长度
[抄题]: [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: "b a " 最后一位是空格,可能误 ...
- LeetCode 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- Java [Leetcode 58]Length of Last Word
题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...
- leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法
Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space charact ...
- [Leetcode] Length of last word 最后一个单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the le ...
- lintcode:Length of Last Word 最后一个单词的长度
题目: 最后一个单词的长度 给定一个字符串, 包含大小写字母.空格' ',请返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 样例 给定 s = "Hello World& ...
随机推荐
- svg path 解析
<pre><svg width="100%" height="100%" version="1.1" xmlns=&quo ...
- Knative 基本功能深入剖析:Knative Serving 的流量灰度和版本管理
作者|冬岛 阿里云技术专家 本篇主要介绍 Knative Serving 的流量灰度,通过一个 rest-api 的例子演示如何创建不同的 Revision.如何在不同的 Revision 之间按照流 ...
- 【题解】Informacije [COCI2012]
[题解]Informacije [COCI2012] 传送门:官方题面 [题目描述] 有一个长度为 \(n\) 的 序列 \(a\)(由 \([1,n]\) 中的数组成,且每个数只会出现一次),现给出 ...
- http接口和webservice接口的区别
web service(SOAP)与HTTP接口的区别 什么是web service? soap请求是HTTP POST的一个专用版本,遵循一种特殊的xml消息格式Content-type设置为: t ...
- 在 React 项目中引入 GG-Editor 编辑可视化流程
蚂蚁金服数据可视化团队曾经开源了一款G6-Editor,但后来停止了对外支持,学习成本太高 好在后来他们团队的大牛高力结合 React + G6 开源了一个 GG-Editor(其实就是G6-Edit ...
- 信息收集利器:ZoomEye
前言 ZoomEye是一款针对网络空间的搜索引擎,收录了互联网空间中的设备.网站及其使用的服务或组件等信息. ZoomEye 拥有两大探测引擎:Xmap 和 Wmap,分别针对网络空间中的设备及网站, ...
- 【原】Spring测试类代码
package test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.bea ...
- Mac OSX(Mac OS10.11) 安装 pwntools 失败的最新解决方案
pwntools是一个 CTF 框架和漏洞利用开发库,用 Python 开发,由 rapid 设计,旨在让使用者简单快速的编写 exploit. 网上针对 Mac OS 的安装教程大多都是基于 pip ...
- 实用的生成二维码API
推荐一个生成二维码的API网站: 中文 http://www.topscan.com 英文 https://api.qrserver.com/v1/create-qr-code/?size=150x1 ...
- 简单使用:SpringBoot整合Redis
1.导入依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...