leetcode || 58、Length of Last Word
problem:
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
.
thinking:
(1)充分理解题意是解这道题的关键。
对于一些特殊情况: char *s='abc " (最后一个是空格), char *s = "a b "(连续空格)要注意到
(2)刚開始考虑使用 指针之差 来计算字符串的长度。发现掉入了一个无底深渊。各种特殊情况都要考虑在内,能解出来,可是比較绕
(3)採用 计数法 来解这道题最简单高效,遇到连续的非空格字符串開始计数,遇到空格保存计数结果,也有一些细节要注意。在代码中有凝视
(4)这道题考擦的是 分析问题的全面性 和 细节处理能力
code:
class Solution {
public:
int lengthOfLastWord(const char *s) {
int i=0;
int count=0;
int length=0;
while(*(s+i)!='\0')
{
if(*(s+i)!=' ')
count++;
else
{
if(count>0) //出现连续空格时,防止清空上次有效计数结果
length=count;
count=0;
}
i++;
}
if(count!=0) //善后处理:以非空格结束的字符串
return count;
else
return length; //以空格结束 }
};
leetcode || 58、Length of Last Word的更多相关文章
- Leetcode(58)题解:Length of Last Word
https://leetcode.com/problems/length-of-last-word/ 题目: Given a string s consists of upper/lower-case ...
- LeetCode OJ:Length of Last Word(最后一个词的长度)
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 【算法】LeetCode算法题-Length Of Last Word
这是悦乐书的第155次更新,第157篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第14题(顺位题号是58).给定一个字符串,包含戴尔字母.小写字母和空格,返回最后一个单 ...
- 【LeetCode算法-58/66】Length of Last Word/Plus One
LeetCode第58题: Given a string s consists of upper/lower-case alphabets and empty space characters ' ' ...
- [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
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
一天一道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 ...
随机推荐
- 【spring cloud】【IDEA】【maven】spring cloud多模块在idea上使用maven插件打包报错:程序包XXX不存在
>>>>spring cloud 多模块 >>>>在idea上使用maven插件打包,欲打包成jar包后 进行部署 >>>> 报 ...
- 【ELK】【docker】【elasticsearch】1. 使用Docker和Elasticsearch+ kibana 5.6.9 搭建全文本搜索引擎应用 集群,安装ik分词器
系列文章:[建议从第二章开始] [ELK][docker][elasticsearch]1. 使用Docker和Elasticsearch+ kibana 5.6.9 搭建全文本搜索引擎应用 集群,安 ...
- Java开发工具全面比较
1.JDK (Java Development Kit)Java开发工具集 从初学者角度来看Java开发工具,采用JDK开发Java程序能够很快理解程序中各部分代码之间的关系,有利于理解Java面向对 ...
- 算法:四种冒泡排序(Bubble Sort)实现
背景 大学关于排序的算法,好像就学会了冒泡排序,这个算是排序界的 hello,world 了,冒泡排序的定义如下: 重复的遍历数组. /// <summary> /// 重复的遍历数组. ...
- html在线编辑器汇总
1. TinyMCE 免费,开源,轻量的在线编辑器,基于 JavaScript,高度可定制,跨平台. 2. FCKEditor 免费,开源,用户量庞大的在线编辑器,有良好的社区支持. 3. YUI E ...
- 在MyEclipse中修改类不重启tomcat
今天因为在调试一个程序,因为工程中用到spring,每次修改类代码时都要重启服务器,搞得很郁闷,于是上网找找有没有可以让java代码每次修改之后 直接加载到服务器的,找了一些还果真有,不过有些方法我试 ...
- kafka深入研究(六)
Kafka Producer端封装自定义消息 Kafka.network包源码解读 Kafka Consumer端的一些解惑 Kafka producer使用注意 kafka0.8的一些变动,先收藏, ...
- 2008技术内幕:T-SQL语言基础 单表查询摘记
这里的摘抄来自<Microsoft SQL Server 2008技术内幕:T-SQL语言基础>,书中用到的案例数据库是这个 TSQLFundamentals2008 ,官网给出的连接是这 ...
- 《mahout实战》
<mahout实战> 基本信息 原书名:Mahout in action 作者: (美)Sean Owen Robin Anil Ted Dunning Ellen Fr ...
- linux编程合并多个静态库.a为一个.a
.a 文件的结构和.tar文件就没有什么区别. x 命令解出来, a 命令添加, t命令列表 假设A.a, B.a C.a 在/usr/local/lib目录下 mkdir /tmp/libABC c ...