【LeetCode-面试算法经典-Java实现】【058-Length of Last Word (最后一个单词的长度)】
【058-Length of Last Word (最后一个单词的长度)】
【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】
原题
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.
题目大意
给定一个由大写和小写字母组和空格组成的字符串,返回字符串中的最后一个单词长度。
解题思路
先从后找第一个字母的位置x,假设没有找到就返回0,假设找到,再找第一个空格的位记为y(y可能是-1。由于没有找到空格),返回结果x-y。
代码实现
算法实现类
public class Solution {
public int lengthOfLastWord(String s) {
int index = s.length() - 1;
// 从后面向前找第一个不是' '的字符
while (index >=0 && s.charAt(index) == ' ') {
index--;
}
if (index < 0) {
return 0;
}
int tmp = index;
// 运行到以下说明存在最后一个单词
// 从后面向前找第一个是' '的字符
while (index >=0 && s.charAt(index) != ' ') {
index--;
}
return tmp - index;
}
}
评測结果
点击图片,鼠标不释放。拖动一段位置。释放后在新的窗体中查看完整图片。
特别说明
欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47164433】
【LeetCode-面试算法经典-Java实现】【058-Length of Last Word (最后一个单词的长度)】的更多相关文章
- 058 Length of Last Word 最后一个单词的长度
给定一个字符串, 包含大小写字母.空格 ' ',请返回其最后一个单词的长度.如果不存在最后一个单词,请返回 0 .注意事项:一个单词的界定是,由字母组成,但不包含任何的空格.案例:输入: " ...
- [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& ...
- 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 ...
- 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】
[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...
- 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】
[053-Maximum Subarray(最大子数组和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Find the contiguous subarray w ...
随机推荐
- Android自己定义控件系列三:自己定义开关button(二)
接上一篇自己定义开关button(一)的内容继续.上一次实现了一个开关button的基本功能.即自己定义了一个控件.开关button,实现了点击切换开关状态的功能.今天我们想在此基础之上.进一步实现触 ...
- linux含有某字符串的文件
find .|xargs grep -ri "IBM" -l 只列出文件名:grep -rn "Item" * -l pattern files
- oracle数据泵备份与还原
完整的常用的一套oracle备份以及还原方案 --在新库中新建数据目录,我没有特别说明在哪执行的语句都可在plsql中执行 CREATE OR REPLACE DIRECTORY dump_dir A ...
- Binary Indexed Tree 总结
特点 1. 针对 数组连续子序列累加和 问题(需要进行频繁的 update.sum 操作): 2. 并非是树型结构,只是逻辑上层次分明: 3. 可以通过 填坑法 来理解: 4. 中心思想:每一个整数都 ...
- 子线程创建AlertDialog错误
Can't create handler inside thread that has not called Looper.prepare()
- oracle常规操作
dbms_job.broken(job1,true);
- spring使用注解开发
1.准备工作(1)导入common-annotations.jar(2)导入schema文件 文件名为spring-context-2.5.xsd(3)在xml的beans节点中配置 service层 ...
- Codeforces Round #506 (Div. 3) A-C
CF比赛题解(简单题) 简单题是指自己在比赛期间做出来了 A. Many Equal Substrings 题意 给个字符串t,构造一个字符串s,使得s中t出现k次;s的长度最短 如t="c ...
- [读书笔记] R语言实战 (五) 高级数据管理
1. 数值函数 1) 数学函数 2) 统计函数 3. 数据标准化 scale() 函数对矩阵或者数据框的指定列进行均值为0,标准化为1的标准化 mydata <- data.frame(c1=c ...
- [学习笔记] CS131 Computer Vision: Foundations and Applications:Lecture 4 像素和滤波器
Background reading: Forsyth and Ponce, Computer Vision Chapter 7 Image sampling and quantization Typ ...