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 ...
随机推荐
- request doesn't contain a multipart/form-data or multipart/mixed stream ……
有文件控件"file"的表单,在提交的时候,直接使用了ajax提交,结果报了一堆错,原来这个东东要提交表单,还要用post方式,最后更改为: $("#saveForm&q ...
- 20160418 while,switch,do..while的使用
9 一.While循环 示例:求100以内所有数的和 Int i=1;//初始条件 Int sum=0; While(i<=100)//循环条件 { Sum+=i;//循环体 i++;//状态改 ...
- Yii2.0数据库查询实例(三)
常用查询: // WHERE admin_id >= 10 LIMIT 0,10 User::find()->])->offset()->limit()->all() / ...
- 每天一个Linux命令(61)killall命令
killall命令用进程的名字来杀死进程. (1)用法: 用法: killall [ -egiqvw ] [ -signal ] [进程名称] 格式:killall -< ...
- SpringBoot2.1.0 application.properties配置
# =================================================================== # COMMON SPRING BOOT PROPERTIE ...
- 【HackerRank】 Sherlock and The Beast
Sherlock and The Beast Sherlock Holmes is getting paranoid about Professor Moriarty, his archenemy. ...
- 【leetcode刷题笔记】Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- jni 编译错误error: unknown type name '__va_list'
platforms\android-9\arch-arm\usr\include\stdio.h:257:37: error: unknown type name '__va_list' 解 ...
- 网络:W5500抓包TCP segment of a reassembled PDU
1.问题描述 W5500 http测试,用wireshark抓包,发现出现很多TCP segment of a reassembled PD. 2. 问题分析 TCP segment of a rea ...
- 我到 vim 配置文件---------修改从---http://www.cnblogs.com/ma6174/archive/2011/12/10/2283393.html
""""""""""""""""&quo ...