第五周:58. Length of Last Word
来源:互联网 发布:iwatch软件 编辑:程序博客网 时间:2024/06/11 07:57
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
.
解题思路:从后面往前遍历,定义res记录字符的个数,如果是字符,res自增,遇到空格就跳出循环,返回res就为最后一个单词的长度,注意一点就是当最后一个字符为空格时,不应把它当做一个英文单词。
代码:
int lengthOfLastWord(char* s) { int slen=strlen(s);int res=0;while(s[slen-1]==' ')slen--;for(int i=slen-1;i>=0;i--){if(s[i]==' ')break;res++;}return res;}
0 0
- 第五周:58. Length of Last Word
- 58. Length of Last Word(第五周)
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- Android多媒体—拍照Camera
- HDU 1003 Max Sum
- 树莓派3 基于Python的pybluez库查询蓝牙
- CSS3免写前缀兼容各大浏览器的JS插件——prefixfree.min.js
- 2016zzuli校赛G题 《蛤玮点菜》(中途相遇法)
- 第五周:58. Length of Last Word
- 华为oj初级 DNA序列
- 这个大学念的完美!我只想收藏下!
- dubbo源码浅析(二)-标签解析
- Design Pattern(3)-State Pattern
- git上传到一半最后报error:failed to push some refs to 'git@github.com:yourname/repositoryname.git'
- 表格花式效果
- html5标签
- 第六届蓝桥杯 省赛 JavaB组 立方变自身