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 ...
随机推荐
- Android加密解密
随笔分类 - Android加密解密 Android数据加密之异或加密算法 摘要: 前言: 这几天被公司临时拉到去做Android IM即时通信协议实现,大致看了下他们定的协议,由于之前没有参与,据说 ...
- 微服务中的 API 网关(API Gateway)
API 网关(API Gateway)提供高性能.高可用的 API 托管服务,帮助用户对外开放其部署在 ECS.容器服务等云产品上的应用,提供完整的 API 发布.管理.维护生命周期管理.用户只需进行 ...
- tf.argmax
tf.argmax(input, axis=None, name=None, dimension=None) Returns the index with the largest value acro ...
- 【翻译自mos文章】Windows平台下的 Oraagent Memory Leak
来源于: Oraagent Memory Leak (文档 ID 1956840.1) APPLIES TO: Oracle Database - Enterprise Edition - Versi ...
- 升级Https前的可行性验证(一)
升级Https之前的可行性验证 注意:自签证书和Nginx的安装都基于ContOS 6 一.如何申请OpenSSL自签证书 1.安装OpenSSL (一)OpenSSL 工具下载 下载地址 (二)Op ...
- 使用DebugView小工具调试已部署的.net程序 (转)
DebugView for Windows能够捕捉Debug输出的信息在本地的操作系统上.如何你需要调试程序有网络访问推荐使用Wireshark和监听HTTP的工具Fiddler. 下载下来是一个ZI ...
- AccessText热键的使用
AccessText可以用于Label与别的控件(常用于TextBox)绑定热键.也可以单独给别的控件设置热键 1.可以在label中使用AccessText 代码: <Label Horizo ...
- C#基础知识整理:C#类和结构(1)
1.结构功能特性? 实现代码?结构用struct关键字定义的,与类类似,但有本质区别.结构实质是一个值类型,它不需要对分配的.结构的特性:(1).结构作为参数传递时,是值传递.(2).结构的构造函数必 ...
- PLSQL Developer连接远程Oracle数据库
要连接远程数据库,传统的一定可行的方法是在本地装一个oracle.然后使用"Network Configuration Assistant"配置.之后用PL/SQL Dev连接.由 ...
- jquery操作select(取值,设置选中)(转)
http://www.cnblogs.com/liaojie970/p/5210541.html 比如<select class="selector"></sel ...