58. Length of Last Word(easy, 字符串问题)
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.
我的方法是:
- 先消除字符串首、尾的空格;
- 再从字符串尾向首查找,直到找到空格或者指针到队首都没找到空格为止,返回最后一个 word 的长度.
我方法,我代码:
\(O(n)\) time, \(O(1)\) extra space.
// 解题关键:消除 s 的首尾空格
int lengthOfLastWord(string s) {
const int n = s.size() - 1;
if (s.size() == 0) return 0;
int begin = 0, end = n;
//消除字符串首尾空格
if (s[0] == ' ') {
for (int i = 0; i < s.size(); i++) {
if (s[i] != ' ') {
begin = i;
break;
}
}
}
if (s[n] == ' ') {
for (int i = n; i >= 0; i--) {
if (s[i] != ' ') {
end = i;
break;
}
}
}
// 从尾部开始查找
for (int i = end; i >= begin; i--) {
if (s[i] == ' ') return end - i;
else if (i == begin) return end - begin + 1;
}
}
58. Length of Last Word(easy, 字符串问题)的更多相关文章
- LeetCode练题——58. Length of Last Word
1.题目 58. Length of Last Word——Easy Given a string s consists of upper/lower-case alphabets and empty ...
- [LeetCode] 58. 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
题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...
- Java [Leetcode 58]Length of Last Word
题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...
- 58. Length of Last Word【leetcode】
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 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】58. Length of Last Word 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 双指针 单指针 日期 题目地址:https: ...
- 【LeetCode】58 - Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
随机推荐
- istio入门(05)istio的架构概念2
- hdu-3348 coins---贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3348 题目大意: 给你一个价格,还有面值分别为1,5,10,50,100(单位:毛)纸币的数量,要你 ...
- 'gbk' codec can't encode character解决方法
使用Python写文件的时候,或者将网络数据流写入到本地文件的时候,大部分情况下会遇到:UnicodeEncodeError: 'gbk' codec can't encode character ' ...
- javascript实现图片延迟加载方法汇总(三种方法)
看到一些大型网站,页面如果有很多图片的时候,当你滚动到相应的行时,当前行的图片才即时加载的,这样子的话页面在打开只加可视区域的图片,而其它隐藏的图片则不加载,一定程序上加快了页面加载的速度,跟着小编一 ...
- Django REST framework+Vue 打造生鲜超市(八)
九.个人中心功能开发 9.1.drf的api文档自动生成和 (1) url #drf文档,title自定义 path('docs',include_docs_urls(title='仙剑奇侠传')), ...
- disabled OR readonly
1.对元素设置disabled以及readonly属性 $("#uid").attr("disabled",true); $("#uid") ...
- [LeetCode] Number Of Corner Rectangles 边角矩形的数量
Given a grid where each entry is only 0 or 1, find the number of corner rectangles. A corner rectang ...
- JS基本数据类型(typeof的返回结果)
number(Infinity/NaN) string boolean function object(null.各种值装箱对象.内置对象.自定义对象) undefined 判断对象是否为某个[类/构 ...
- 【bzoj4444 scoi2015】国旗计划
题目描述 A 国正在开展一项伟大的计划 —— 国旗计划.这项计划的内容是边防战士手举国旗环绕边境线奔袭一圈.这项计划需要多名边防战士以接力的形式共同完成,为此,国土安全局已经挑选了 NN 名优秀的边防 ...
- 【bzoj4011 hnoi2015】落忆枫音
题目描述 「恒逸,你相信灵魂的存在吗?」 郭恒逸和姚枫茜漫步在枫音乡的街道上.望着漫天飞舞的红枫,枫茜突然问出这样一个问题. 「相信吧.不然我们是什么,一团肉吗?要不是有灵魂......我们也不可能再 ...