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 ...
随机推荐
- Material Designer的低版本兼容实现(三)——Color
在Material Designer中,色彩再一次被摆到了重要的位置上.官方文档中竟然给出了500种配色方案进行选择.就是为了给不同的手机.电视.手表上带来一直的用户体验. 更多用于控制色彩的属性,可 ...
- SpringBoot 获取当前登录用户IP
控制器方法: @RequestMapping(value = "/getIp", method = RequestMethod.POST) @ResponseBody public ...
- 使用SpringBoot的关于页面跳转的问题
示例如下: @Controller public class UserController { @Resource UserService userService; @RequestMapping(& ...
- [Hook] 跨进程 Binder 学习指南
cp from : http://weishu.me/2016/01/12/binder-index-for-newer/ 毫不夸张地说,Binder是Android系统中最重要的特性之一:正如其名“ ...
- [转]Linux系统下如何查看及修改文件读写权限
转自 :http://www.cnblogs.com/CgenJ/archive/2011/07/28/2119454.html 查看文件权限的语句: 在终端输入:ls -l xxx.xxx (xxx ...
- Flyweight 享元模式 MD
享元模式 简介 在JAVA语言中,String类型就是使用了享元模式,JAVA中的字符串常量都是存在常量池中的,JAVA会确保一个字符串常量在常量池中只有一个拷贝,避免了在创建N多相同对象时所产生的不 ...
- javascript运算符instanceof
概述 instanceof 运算符可以用来判断某个构造函数的prototype属性是否存在另外一个要检测对象的原型链上. 语法 object instanceof constructor 参数 obj ...
- Android adb你真的会用吗?
引言 本文基于Android官方文档, 以及个人工作的使用经验, 总结下adb的常用用法, 备忘. 1, adb简介 adb全名Andorid Debug Bridge. 顾名思义, 这是一个Debu ...
- iOS中的时钟动画
iOS 动画效果非常多,我们在开发中可能会遇到很多动画特效,我们就会用到核心动画框架CoreAnimation,核心动画里面的动画效果有很多,都是在QuartzCore.framework框架里面,今 ...
- Windows10 安装Jupyter
官方文档:https://jupyter-notebook.readthedocs.io/en/stable/ https://github.com/jupyter/jupyter/wiki/A-ga ...