当前位置:网站首页>leetcode之最后一个单词的长度
leetcode之最后一个单词的长度
2020-11-09 23:48:00 【go4it】
序
本文主要记录一下leetcode之最后一个单词的长度
题目
给定一个仅包含大小写字母和空格 ' ' 的字符串 s,返回其最后一个单词的长度。如果字符串从左向右滚动显示,那么最后一个单词就是最后出现的单词。
如果不存在最后一个单词,请返回 0 。
说明:一个单词是指仅由字母组成、不包含任何空格字符的 最大子字符串。
示例:
输入: "Hello World"
输出: 5
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/length-of-last-word
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解
class Solution {
public int lengthOfLastWord(String s) {
int result = 0;
char[] chars = s.toCharArray();
for (int i= s.length()-1; i >=0; i--) {
if (chars[i] != ' ') {
result++;
continue;
}
if (result != 0) {
return result;
}
}
return result;
}
}
小结
这里从后往前遍历字符串数组,遇到非空格的累加长度,遇到空格则判断结果是否为0,不为0则返回结果。
doc
版权声明
本文为[go4it]所创,转载请带上原文链接,感谢
https://my.oschina.net/go4it/blog/4710388
边栏推荐
- C/C++编程日记:逻辑井字棋(圈叉)游戏开发
- C / C + + Programming Notes: C language development tank war! In memory of our lost little overlord game
- CUDA_ Shared memory, memory access mechanism, access optimization
- Visit 2020 PG Technology Conference
- Hengxun Technology: the way to deal with server downtime
- 那么当下的行情投资者该怎么办呢? 现在新的投资风口来了!
- IP地址SSL证书
- 没有磁盘空间 No space left on device
- 公网IP地址和SSL证书可以提升SEO吗?
- 团灭 LeetCode 打家劫舍 问题
猜你喜欢

代码中的软件工程--对menu项目的源码分析

What can CRM system help enterprises do?

C / C + + Programming Notes: C language development tank war! In memory of our lost little overlord game

毕业设计之 ---基于微服务框架的电影院订票系统

Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom

商品后台系统优化

November 09, 2020: talk about the similarities and differences between the bulon filter and the cuckoo filter?

Software engineering in code -- source code analysis of menu project

Functional guide for temporary users and novices of PL / SQL developer

SSL证书和公网IP地址如何影响SEO
随机推荐
SQL server attached database access denial resolution summary
LeetCode 50 Pow(x,n)
Traditional purchasing mode has changed! How to innovate automobile purchasing function?
SQL case conversion, remove the space before and after
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
爱康国宾怒斥国信证券报告失实,已发律师函
Come and learn! Development Guide for personalized recommendation system (with internet disk link)
团灭 LeetCode 打家劫舍 问题
Win7 + vs2015 + cuda10.2 configuration tensorrt7.0
如何k个一组反转链表
手把手教你使用容器服务 TKE 集群审计排查问题
[best practice] learn how eolinker helps Telecommuting
AQS source code in-depth analysis of conditional queue
毕业即失业?大学生如何分配学习时间比例,拥有完整计算机知识体系?
Gets the property value of a column in the list collection object
SSL证书和公网IP地址如何影响SEO
剑指offer之打印超过数组一半的数字
On the practical application of C 9's new features
SQL filter query duplicate columns
Validation failed for one or more entities. See ‘EntityValidationErrors’解决方法