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 ...
随机推荐
- Effective C++笔记03:资源管理
资源:动态分配的内存.文件描写叙述器.相互排斥锁.图形界面中的字型与笔刷.数据库连接以及网络sockets等,不管哪一种资源,重要的是,当你不再使用它时,必须将它还给系统. 条款13:以对象管理资源 ...
- iOS7以下设备获取mac地址
注意,是iOS7以下的设备,直接上源码,获取mac地址都是为了唯一标识一个设备的,但iOS7设备的mac地址为 020000000000 MacAddress.h #include <sys/s ...
- java获取http请求的Header和Body
在http请求中,有Header和Body之分,读取header使用request.getHeader("..."); 读取Body使用request.getReader(),但g ...
- 使用web.xml方式加载Spring时,获取Spring context的两种方式
使用web.xml方式加载Spring时,获取Spring context的两种方式: 1.servlet方式加载时: [web.xml] <servlet> <servlet-na ...
- word转pdf图片问题
经过整理总结出两类问题:1,pdf文件下载文档中某些图片显示红叉. 问题现象:pdf是通过word转换成,发现源文件doc和docx文档均出现上述问题:只是某些图片显示红叉.通过这两点确定和文 ...
- WebService—CXF整合Spring实现接口发布和调用过程
一.CXF整合Spring实现接口发布 发布过程如下: 1.引入jar包(基于maven管理) <!-- cxf --> <dependency> <groupId> ...
- 【XJOI】【NOI考前模拟赛7】
DP+卡常数+高精度/ 计算几何+二分+判区间交/ 凸包 首先感谢徐老师的慷慨,让蒟蒻有幸膜拜了学军的神题.祝NOI2015圆满成功 同时膜拜碾压了蒟蒻的众神QAQ 填填填 我的DP比较逗比……( ...
- scala编程第18章学习笔记——有状态的对象
银行账号的简化实现: scala> class BankAccount{ | private var bal: Int = 0 | def balance: Int = bal | def de ...
- 好久没做codeforces
近期小结: 做了四场多校的比赛,感觉学到的东西好少诶,除了CLJ那场太神,其他场次的赛后几乎都能独立的AK 感觉顶多就锻炼锻炼代码能力?真是件伤感的事情... 虽然每场都,b,但只要baolaoban ...
- System.Windows.Forms.Control : Component, IOleControl, IOleObject, IOleInPlaceObject, IOleInPlaceActiveObject....
#region 程序集 System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ...