LeetCode OJ: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.
求最后一个非空单词的长度而已,没什么注意点,直接看代码,理解题目意思就可以了:
class Solution {
public:
int lengthOfLastWord(string s) {
int len = s.length();
if(len == ) return ;
while(s[len - ] == ' ')
if(len - >= )
len--;
else
return ;
len--;
int lenCount = ;
while(s[len] != ' ' && len >= ){
lenCount++, len--;
}
return lenCount;
}
};
java版本的代码如下所示,倒过来读很容易了:
public class Solution {
public int lengthOfLastWord(String s) {
int i = s.length() - 1;
while(i >= 0 && s.charAt(i) == ' ')
i--;
int end = i+1;
while(i >= 0 && s.charAt(i) != ' ')
i--;
int start = i+1;
return end - start;
}
}
LeetCode OJ:Length of Last Word(最后一个词的长度)的更多相关文章
- [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 ...
- [Leetcode] Length of last word 最后一个单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the le ...
- lintcode:Length of Last Word 最后一个单词的长度
题目: 最后一个单词的长度 给定一个字符串, 包含大小写字母.空格' ',请返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 样例 给定 s = "Hello World& ...
- 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 字符串
找出最后一个词的长度 class Solution { public: int lengthOfLastWord(string s) { , l = , b = ; while((b = s.find ...
- 【LeetCode】- Length of Last Word(最后一个单词的长度)
[ 问题: ] Given a string s consists of upper/lower-case alphabets and empty space characters ' ', retu ...
随机推荐
- windows下redis的安装和启动
Rides: //cmd管理员进入 // 运行 : redis-cli.exe //报错 :Redis (error) NOAUTH Authentication required.解决方法 // ...
- 使用Kotlin开发Android应用(I):简介
Kotlin是一门基于JVM的编程语言,它正成长为Android开发中用于替代Java语言的继承者.Java是世界上使用最多的编程语言之一,当其他编程语言为更加便于开发者使用而不断进化时,Java并没 ...
- 【Network】DDoS攻击防御
DDoS(Distributed Denial of Service,分布式拒绝服务)攻击的主要目的是让指定目标无法提供正常服务,甚至从互联网上消失,是目前最强大,最难防御的攻击之一. 按照发起的方式 ...
- iOS应用生命周期
作为应用程序的委托对象,AppDelegate类在应用生命周期的不同阶段会回调不同的方法.首先,让我们先了解一下iOS 应用的不同状态及它们彼此间的关系,见图1 . 图1 iOS应用状态图 下面简要介 ...
- PsySH——PHP交互式控制台
PsySH PsySH is a runtime developer console, interactive debugger and REPL for PHP. PsySH是一个PHP的运行时开发 ...
- iOS 学习如何声明私有变量和私有方法
私有变量 首先来说 OC 中没有绝对的私有变量,这么说基于两点原因: 1可修改: 通过KVC 键值编码 来修改私有成员变量的值 2可读取 : 通过底层runtime 获取实例变量Ivar 对应 ...
- asp.net Cookie 用户登陆时记住我
/// <summary> /// 判断Cookie中存储的数据 /// </summary> protected void CheckUserCookie() { //先判断 ...
- 计算机网络概述 传输层 TCP流量控制
TCP流量控制 所谓流量控制就是让发送发送速率不要过快,让接收方来得及接收.利用滑动窗口机制就可以实施流量控制.通过运用TCP报文段中的窗口大小字段来控制,发送方的发送窗口不可以大于接收方发回的窗口大 ...
- java图形验证码
用java实现验证码的生成,以下代码是一个controller,可以直接使用 package org.jxnd.tongxuelu.controller; import java.awt.Color; ...
- 八、linux优化一
1.关闭selinux sed –I ‘s#SELINUX=enforcing#SELINUX=disabled#g’ /etc/selinux/config grep SELINUX=disable ...