<LeetCode OJ> 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) {
int strLen=s.size();
int len=0;
int i=0;
while(s[strLen-i-1]==' ')
i++;
for(;i<strLen;i++)
{
if(s[strLen-i-1]>='A'&&s[strLen-i-1]<='Z' || s[strLen-i-1]>='a'&&s[strLen-i-1]<='z' )
len++;
else
break;
}
return len;
}
};
或者用STL来写:
class Solution {
public:
int lengthOfLastWord(string s) {
int i = s.find_last_not_of(' ');
return (i == string::npos) ? 0 : (i - s.find_last_of(' ', i));
}
};
注:本博文为EbowTang原创。兴许可能继续更新本文。假设转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50498956
原作者博客:http://blog.csdn.net/ebowtang
<LeetCode OJ> 58. Length of Last Word的更多相关文章
- 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❤python】 58. Length of Last Word
#-*- coding: UTF-8 -*-#利用strip函数去掉字符串去除空格(其实是去除两边[左边和右边]空格)#利用split分离字符串成列表class Solution(object): ...
- [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
一天一道LeetCode系列 (一)题目 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 ...
- 【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 ...
- 58. Length of Last Word【leetcode】
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
随机推荐
- (二): 基于ZeroMQ的实时通讯平台
基于ZeroMQ的实时通讯平台 上篇:C++分布式实时应用框架 (Cpp Distributed Real-time Application Framework)----(一):整体介绍 通讯平台作为 ...
- GCC 中的编译器堆栈保护技术
GCC 中的编译器堆栈保护技术 前几天看到的觉得不错得博客于是转发了,但这里我补充一下一些点. GCC通过栈保护选项-fstack-protector-all编译时额外添加两个符号,__stack_c ...
- 线上Mysql数据库崩溃事故的原因和处理
前文提要 承接前文<一次线上Mysql数据库崩溃事故的记录>,在文章中讲到了一次线上数据库崩溃的事件记录,建议两篇文章结合在一起看,不至于摸不着头脑. 由于时间原因,其中只讲了当时的一些经 ...
- mapreduce解析执行sql流程
样例准备 编号 姓名 性别 班级编号 1 name_1 male 1 2 name_2 female 2 3 name_3 male 3 4 name_4 female 4 5 name_5 male ...
- 在moba游戏里面模拟实现绝地求生毒雾圈功能
---恢复内容开始--- 已经有很长一段时间没做项目了上半年大多数时间都荒废在王者荣耀,哈哈,S8赛季也上了王者,美滋滋 转回正题,公司需要开个新项目,需要把原有的moba游戏,改成类似绝地求生那玩法 ...
- [转载] Java中动态加载jar文件和class文件
转载自http://blog.csdn.net/mousebaby808/article/details/31788325 概述 诸如tomcat这样的服务器,在启动的时候会加载应用程序中lib目录下 ...
- "虐待"过我的老师们,你们如今还好吗
当皇城脚下的民生问题都这么难做的时候,其他地方又该如何保障呢? 京城“红黄蓝”出名了,京城发生锅炉大爆炸了,京城发生火灾了…… 聊天中,有一好友突然蹦出了一句话:“你看在皇城脚下都不安全了”. 久久我 ...
- Mac下Kali虚拟机与宿主机共享文件夹
宿主机: 1.创建文件夹.测试文件 ZhangSan-MacBook-Air:~ zhangsan$ mkdir kalishare && cd kalishare/ ZhangSan ...
- 十二、VueJs 填坑日记之项目打包发布
通过上一篇博文的学习,我们其实已经完成了我们设想的项目的开发.但是,我们做好的这套东西,是基于 nodejs 开发的.而我们最终希望,我们开发的项目,生成好一堆文件,然后随便通过任何一个 http 服 ...
- c语言的内存分析
1. 进制 1. 什么是进制 ● 是一种计数的方式,数值的表示形式 汉字:十一 十进制:11 二进制:1011 八进制:13 ● 多种进制:十进制.二进制.八进制.十六进制.也就是说,同一个 ...