【leetcode】length of last word (easy)
题目: 输入字符串 s,返回其最后一个单词的长度
如 s="Hello World" 返回5
s="Hello World " 返回5
s=" " 返回0
开始从前向后判断,超时了。改成从后向前判断,通过了。
class Solution {
public:
int lengthOfLastWord(const char *s) {
int length = ;
int slen = strlen(s);
for(int i = slen -; i >= ; i--)
{
if(s[i] == ' ')
slen--;
else
break;
}
for(int i = slen - ; i >=; i--)
{
if(s[i] == ' ')
break;
else
length++;
}
return length;
}
};
【leetcode】length of last word (easy)的更多相关文章
- 【leetcode】Length of Last Word
题目简述 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...
- 【LeetCode】- Length of Last Word(最后一个单词的长度)
[ 问题: ] Given a string s consists of upper/lower-case alphabets and empty space characters ' ', retu ...
- 【LEETCODE】69、动态规划,easy,medium级别,题目:198、139、221
package y2019.Algorithm.dynamicprogramming.easy; /** * @ProjectName: cutter-point * @Package: y2019. ...
- 【LeetCode】748. Shortest Completing Word 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】819. Most Common Word 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+统计 日期 题目地址:https://leet ...
- 【leetcode】Maximum Product of Word Lengths
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- 【LeetCode】1003. Check If Word Is Valid After Substitutions 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 循环 日期 题目地址:https://leetcod ...
- 【leetcode】Count and Say (easy)
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- 【leetcode】 Search a 2D Matrix (easy)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- PHP 使用命名空间(namespace),实现自动加载
示例: #/DB/MySql.class.php也就是DB文件夹下有MySql.class.php文件 namespace DB; class MySql { public function __co ...
- Error: [ng:areq] Argument 'xxxx' is not a function, got undefined
"Error: [ng:areq] Argument 'keywords' is not a function, got undefined" 代码类似这样的: <div n ...
- Js设置及获取Cookie的方法
Login页面设置Cookie: <script type="text/javascript"> if(json.result=="true") { ...
- UIGestureRecognizer
•为了完成手势识别,必须借助于手势识别器----UIGestureRecognizer • •利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势 • •UIG ...
- PHP函数preg_replace() 正则替换所有符合条件的字符串
PHP preg_replace() 正则替换,与JavaScript 正则替换不同,PHP preg_replace() 默认就是替换所有符号匹配条件的元素. preg_replace (正则表达式 ...
- Mac Pro 软件收藏
记录下Pro Mac中安装过的软件: 编号 软件名 功能说明 1 QQ 2 微信 3 搜狗输入法 4 Chrome 浏览器 Chrome 及其 插件“个性化设置”备份 5 360云盘 ...
- 谷歌黑科技WaveNet,更先进的语音合成
导读 Google 的 DeepMind 研究实验室昨天公布了其在计算机语音合成领域的最新成果——WaveNet.该语音合成系统能够模仿人类的声音,生成的原始音频质量优于目前的文本转语音系统(text ...
- iOS跳转到另一个程序
我这里只是写了部分东西,如果想看更加详细的,请点击原文链接. 原文链接:http://blog.csdn.net/likendsl/article/details/7553605 原则上iOS的沙 ...
- iOS9开发者测试版下载地址(系转载)
将链接复制后,打开迅雷即可下载!请根据自己的手机型号下载.不需要udid绑定.开发者账号,下载后直接升级即可.请根据自己的手机型号将链接复制 打开迅雷 点击立即下载即可.不过好像Mac版的迅雷下载不了 ...
- HDU 1003 动态规划
http://acm.hdu.edu.cn/showproblem.php?pid=1003 这几天开始刷动归题目,先来一道签到题 然而做的并不轻松, 没有注意到边界问题, WA了几发才发现 #inc ...