Length of Last Word | Leetcode
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.
两个下标,head搜索非空格元素,last搜索head后的第一个空格或者字符串结尾。检测到单词后保存至lastlen里。
class Solution {
public:
int lengthOfLastWord(string s) {
const unsigned int len = s.size();
unsigned int head,last,lastlen;
head = ;
last = ;
lastlen = ;
if (!len)
;
) {
// search for the first non-space character
while ((s[head] == ' ') && (head != len))
head ++;
if (head == len)
return lastlen;
last = head;
while ((last != len) && (s[last] != ' '))
last ++;
if (last == len)
return last - head;
else {
lastlen = last - head;
head = last;
}
}
}
};
Length of Last Word | Leetcode的更多相关文章
- [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
leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...
- 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
一天一道LeetCode系列 (一)题目 Given a string s consists of upper/lower-case alphabets and empty space charact ...
- [leetcode]Length of Last Word @ Python
原题地址:https://oj.leetcode.com/problems/length-of-last-word/ 题意: Given a string s consists of upper/lo ...
- Leetcode(58)题解:Length of Last Word
https://leetcode.com/problems/length-of-last-word/ 题目: Given a string s consists of upper/lower-case ...
- [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/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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 双指针 单指针 日期 题目地址:https: ...
随机推荐
- C# Attribute (上)——Attribute初体验
原始出处 .作者信息和本声明.否则将追究法律责任.http://liutiemeng.blog.51cto.com/120361/29201 小序: 注意:本次小序颇长而且没什么 ...
- [原理][源代码解析]spring中@Transactional,Propagation.SUPPORTS,以及 Hibernate Session,以及jdbc Connection关系---转载
问题: 一. 1. Spring 如何处理propagation=Propagation.SUPPORTS? 2. Spring 何时生成HibernateSession ? 3. propagati ...
- Android 自定义View修炼-【2014年最后的分享啦】Android实现自定义刮刮卡效果View
一.简介: 今天是2014年最后一天啦,首先在这里,我祝福大家在新的2015年都一个个的新健康,新收入,新顺利,新如意!!! 上一偏,我介绍了用Xfermode实现自定义圆角和椭圆图片view的博文& ...
- Android应用程序所包含的四种组件和DDMS
关注用户组件 Activity 编辑文本 .玩游戏 后台进程 Service ...
- 安卓开发中使用Genymotion模拟器
在安卓开发中,运行和调试自己所写的安卓程序需要用到模拟器 在一般情况下 是直接在这创建一个模拟器,但是这种自带的模拟器运行效率不佳,而且启动时间漫长 所以,我们可以换一款安卓模拟器 Genymotio ...
- CentOS7安装小小输入法
添加epel源 $ sudo yum install epel-release 安装centos7缺失依赖包 libwayland-client $ sudo rpm -Uvh ftp://ftp.p ...
- access应用分享
我最近开发了一个winform项目,用的是access数据库,当开发成功以后,有些电脑上就能正常运行,有些电脑就无法读取数据库文件,刚开始我以为是权限问题,后来我把安装程序卸载了,装在其它盘中(非C盘 ...
- jquery无法读取json文件问题
jquery无法读取json文件,如:user.json文件无法读取.把user.json文件的后缀名修改为aspx,文件内容不变,则可以读取~ 原理不懂!~~
- ASP.NET MVC(一) 什么是Razor
Razor 是一种向网页添加基于服务器的代码的标记语法 Razor 不是编程与语言.它是服务端标记语言. 当网页被写入浏览器时,基于服务器的代码能够创建动态内容.在网页加载时,服务器在向浏览器返回页面 ...
- ACM——简单排序
简单选择排序 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交:836 测试通过:259 描述 给定输入排序元素 ...