LeetCode: 58. Length of Last Word(Easy)
1. 原题链接
https://leetcode.com/problems/length-of-last-word/description/
2. 题目要求
给定一个String类型的字符串,字符串中可能含有空格‘ ’。从字符串的末尾开始查找,找到第一个单词,该单词中间不能有空格,并返回其长度。
3. 解题思路
首先判断该字符串是否为空,为空直接返回 0;
否则,从尾部向前查找,使用两个while循环,第一个while循环用于过滤空格,当该位置不为空格时,跳出while循环;
然后进入第二个while循环,依次向前查找,用使用累加器进行记录单词长度。直到当前位置为空格时,跳出while循环,并返回单词长度。
4. 代码实现
public class LengthofLastWord59 {
public static void main(String[] args) {
String s = "";
System.out.println(lengthOfLastWord(s));
} public static int lengthOfLastWord(String s) {
if(s.length()==0) return 0;
int slen = s.length() - 1;
int res = 0; // 过滤空格
while (s.charAt(slen) == ' ' && slen != 0)
slen--;
// 计算单词长度
while (s.charAt(slen) != ' ') {
res++;
if (slen == 0) break;
slen--;
}
return res;
}
}
LeetCode: 58. Length of Last Word(Easy)的更多相关文章
- Leetcode 之Length of Last Word(37)
扫描每个WORD的长度并记录即可. int lengthOfLast(const char *s) { //扫描统计每个word的长度 ; while (*s) { if (*s++ != ' ')/ ...
- Leetcode 之Length of Last Word(38)
做法很巧妙.分成左右两个对应的部分,遇到左半部分则入栈,遇到右半部分则判断对应的左半部分是否在栈顶.注意最后要判断堆栈是否为空. bool isValid(const string& s) { ...
- [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 (最后单词的长度) 解题思路和方法
Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space charact ...
- LeetCode 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- Java [Leetcode 58]Length of Last Word
题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...
- [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 ' ', retu ...
- LeetCode:14. Longest Commen Prefix(Easy)
1. 原题链接 https://leetcode.com/problems/longest-common-prefix/description/ 2. 题目要求 给定一个字符串数组,让你求出该数组中所 ...
随机推荐
- Django 创建model的一些注意事项
自增主键字段¶ 默认情况下,Django 会给每个模型添加下面这个字段: id = models.AutoField(primary_key=True) 这是一个自增主键字段. 如果你想指定一个自定义 ...
- thinkphp清除缓存
前台 //清除缓存 $(function(){ $("#cache").click(function(){ layer.confirm('你确定要清除缓存吗?', {icon: 3 ...
- REG小探
根键名称缩写对照表 常用数据类型
- 为什么 window.location.search 为空?
1,什么是window.location?示例 URL:http://b.a.com:88/index.php?name=kang&when=2011#first 属性 含义 值 protoc ...
- Spring(二十)之使用Log4j记录日志
日志记录,也是常用的,比如异常信息记录或者其他相关信息记录,良好的日志记录有助于当系统出现某些不是特别大的问题时,可及时通过日志信息,捕捉到异常,从而确定是那段代码的问题,避免影响其他的代码. 关于m ...
- 解决FileUpload上传大文件报错
<system.webServer> <security> <requestFiltering> <requestLimits maxAllowedConte ...
- 给windows添加路由
route add 10.0.0.0 mask 255.0.0.0 172.16.1.253 -p
- 报错Caused by: org.hibernate.AnnotationException: No identifier specified for entity:
Caused by: org.hibernate.AnnotationException: No identifier specified for entity:. 原因: 1.没有给实体类ID 解决 ...
- POJ 3216 Prime Path(打表+bfs)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27132 Accepted: 14861 Desc ...
- 高考结束了,在门头沟有没有想学php建站的。
教你简单的html 教你文章后台管理 数据库管理及备份 编程工具的使用 如何找到你要学习的内容