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",
return5.
解法一:
顺序遍历,有单词则len++,有空格且下一位为单词则len=0,直到末尾输出len
class Solution {
public:
int lengthOfLastWord(const char *s) {
int len=;
while(*s)
{
if(*s++!=' ')
len++;
else if(*s&&*s!=' ')
len=;
}
return len;
}
};
解法二:
class Solution {
public:
int lengthOfLastWord(const char *s) {
int count = ;
int len = strlen(s);
//反向查找,末尾空格忽略,行中出现空格就终止循环
for(int i = len-; i >= ; i--){
if(s[i] == ' '){
if(count)
break;
}
else{
count++;
}
}
return count;
}
};
length-of-last-word 最后一个单词的长度的更多相关文章
- lintcode:Length of Last Word 最后一个单词的长度
题目: 最后一个单词的长度 给定一个字符串, 包含大小写字母.空格' ',请返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 样例 给定 s = "Hello World& ...
- [Leetcode] Length of last word 最后一个单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the le ...
- 058 Length of Last Word 最后一个单词的长度
给定一个字符串, 包含大小写字母.空格 ' ',请返回其最后一个单词的长度.如果不存在最后一个单词,请返回 0 .注意事项:一个单词的界定是,由字母组成,但不包含任何的空格.案例:输入: " ...
- 58. Length of Last Word最后一个单词的长度
[抄题]: [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: "b a " 最后一位是空格,可能误 ...
- [LeetCode] 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 ...
- [leetcode]58. 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 ...
- [Swift]LeetCode58. 最后一个单词的长度 | 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(最后一个单词的长度)
[ 问题: ] Given a string s consists of upper/lower-case alphabets and empty space characters ' ', retu ...
随机推荐
- hdu 2049 不easy系列之(4)——考新郎
不easy系列之(4)--考新郎 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 让Android SDK Manager正常更新的办法
首先,进入C:\Windows\System32\drivers\etc,找到hosts文件,用记事本打开它. 在文件的最后加入: 203.208.46.146 dl.google.com 203.2 ...
- java将XML文档转换成json格式数据
功能 将xml文档转换成json格式数据 说明 依赖包:1. jdom-2.0.2.jar : xml解析工具包;2. fastjson-1.1.36.jar : 阿里巴巴研发的高性能json工具包 ...
- 【ContestHunter】【弱省胡策】【Round7】
Prufer序列+高精度+组合数学/DP+可持久化线段树 Magic 利用Prufer序列,我们考虑序列中每个点是第几个插进去的,再考虑环的连接方式,我们有$$ans=\sum_{K=3}^n N^{ ...
- SharePoint 2016 IT Preview的新feature列表
大致看了一下, 其中关于存储的部分貌似没有. 感觉Hybrid in SharePoint 2016未来可以做的事情比较多吧, 必须赶紧弄个cloud的东西开始玩玩了. Search的部署的更新, 以 ...
- ASP,ASP.net,JSP语法、内置对象对比
1 各自的HelloWord版本 1.1 ASP <% Response.Write("hello asp") %> 文件名为test.asp. 1.2 ASP.ne ...
- iOS开发-UILabel和UIButton添加下划线
关于UILabel和UIButton有的时候需要添加下划线,一般有两种方式通过默认的NSMutableAttributedString设置,第二种就是在drawRect中画一条下划线,本文就简单的选择 ...
- iOS开发-音乐播放(AVAudioPlayer)
现在的手机的基本上没有人不停音乐的,我们无法想象在一个没有声音的世界里我们会过的怎么样,国内现在的主流的主流网易云音乐,QQ音乐,酷狗,虾米,天天基本上霸占了所有的用户群体,不过并没有妨碍大家对音乐的 ...
- java log4j日志配置
1.首先看pom.xml文件,需要以下配置 <dependency> <groupId>log4j</groupId> <artifactId>log4 ...
- FastText 文本分类使用心得
http://blog.csdn.net/thriving_fcl/article/details/53239856 最近在一个项目里使用了fasttext[1], 这是facebook今年开源的一个 ...