[LeetCode] 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.
这题看似很简单,本来想睡前切一道怡情,结果切到一点钟,东改西改还是没AC....
因为edge case 很多,考虑一不周全就是错的,比如 "", " ", " a", " a", "a b " 这几个case。
最后我AC的思路是用 i 来记录当前的词长,如果遇到空格则说明此词完毕,把 j = i; i = 0 然后继续直到串尾巴'\0'
最后判断 i 是否为0,如果是则返回j,否则返回i
int lengthOfLastWord(const char *s) {
int i=, j=;
while (*s != '\0') {
if (*s == ' ') {
if (i != ){
j = i;
i = ;
}
}
else {
i++;
}
s++;
}
if (i == ) return j;
return i;
}
囧
[LeetCode] Length of Last Word的更多相关文章
- [LeetCode] 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 ' ', return the l ...
- [leetcode]Length of Last Word @ Python
原题地址:https://oj.leetcode.com/problems/length-of-last-word/ 题意: Given a string s consists of upper/lo ...
- Leetcode: Length of Last Word in python
Length of Last Word Total Accepted: 47690 Total Submissions: 168587 Given a string s consists of ...
- [Leetcode] Length of last word 最后一个单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the le ...
- [LeetCode] 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 最后一个字的长度
class Solution { public: int lengthOfLastWord(const char *s) { ; string snew=s; ,len=strlen(s); ]; ) ...
- leetcode 题解: Length of Last Word
leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...
- 【一天一道LeetCode】#58. Length of Last Word
一天一道LeetCode系列 (一)题目 Given a string s consists of upper/lower-case alphabets and empty space charact ...
随机推荐
- python入门教程链接
python安装 选择 2.7及以上版本 linux: 一般都自带 windows: https://www.python.org/downloads/windows/ mac os: https:/ ...
- Extjs TabPanel 选项卡延迟加载
Extjs TabPanel 选项卡延迟加载 说明: Ext中用到tabpanel选项卡控件, 选项卡页签默认是延迟加载的, 当用户手工切换到某页签下时该页签才会加载, 在页签没有加载前, 用户对该页 ...
- (转)Do not use "using" for WCF Clients - 不要将WCF Client 放在 ‘Using’ 代码块中
RT,最近在编写WCF的应用程序,发现WCF client在关闭的时候有可能会抛出异常,经过搜索之后,发些小伙伴们也遇到过类似的问题,遂记载下来,以备自身和其他小伙伴查看. 原文链接:http://w ...
- Struts2学习笔记《三》
Struts2的工作原理图: struts的用法:
- Google Code Jam 2015 R1C B
题意:给出一个键盘,按键都是大写字母.给出一个目标单词和一个长度L.最大值或者最大长度都是100.现在随机按键盘,每个按键的概率相同. 敲击出一个长度为L的序列.求该序列中目标单词最多可能出现几次,期 ...
- Unity3d《Shader篇》法线贴图
效果图 贴图 法线贴图 //代码 Shader "Custom/NormalMap" { Properties { _MainTex ("Texture", 2 ...
- bccomp比较大小注意
2015年12月15日 14:18:56 星期二 echo bccomp('1', '1.01', 2); // -1 echo bccomp('1', '1.01', 3); // -1 echo ...
- ACM/ICPC 之 BFS-广搜+队列入门-抓牛(POJ3278)
这一题是练习广度优先搜索很好的例题,在很多广搜教学中经常用到,放在这里供学习搜索算法的孩纸们看看= = 题目大意:一维数轴上,农夫在N点,牛在K点,假定牛不会移动,农夫要找到这头牛只能够进行以下三种移 ...
- MongoDB 查询优化分析
摘要: 在MySQL中,慢查询日志是经常作为我们优化查询的依据,那在MongoDB中是否有类似的功能呢?答案是肯定的,那就是开启Profiling功能.该工具在运行的实例上收集有关MongoDB的写操 ...
- java 基础第一季
1. i安装jdk ii 配置环境变量:JAVA_HOME 配置jdk的安装路径 path 配置命令文件的位置 bin目录的安装路径 PATH_HOME 配置库文件的位置 l ...