当前位置:网站首页>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
边栏推荐
- Application of EMQ X in Internet of things platform of China Construction Bank
- LeetCode-378. 有序矩阵中第K小的元素
- crm系统的成本一般是多少?
- IP address SSL certificate
- CRM系统能帮助企业做哪些事?
- 商品后台系统实现
- Win7 + vs2015 + cuda10.2 configuration tensorrt7.0
- PL/SQL Developer临时用户和新手的功能指南
- How SSL certificate and public IP address affect SEO
- Software engineering in code -- source code analysis of menu project
猜你喜欢
Software engineering in code -- source code analysis of menu project
函数计算进阶-IP查询工具开发
How to implement LRU algorithm
Can public IP address and SSL certificate improve SEO?
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
SQL case conversion, remove the space before and after
SQL server attached database access denial resolution summary
Kubernetes-18: installation and use of dashboard
C / C + + Programming Notes: C language development tank war! In memory of our lost little overlord game
CUDA_ Memory model
随机推荐
2020-11-09:谈谈布隆过滤器和布谷鸟过滤器的相同点和不同点?
团灭 LeetCode 打家劫舍 问题
Application of EMQ X in Internet of things platform of China Construction Bank
CRM系统能帮助企业做哪些事?
Baishan cloud technology is selected as the top 100 Internet enterprises in China in 2020
Leetcode 49 letter heterotopic word grouping
How to make a set of K reverse linked lists
The solution of type interface * * is not known to the mapperregistry
mongodb内核源码实现、性能调优、最佳运维实践系列-command命令处理模块源码实现一
函数计算进阶-IP查询工具开发
Python中[:]与[::]的用法
jt-京淘项目
Kubernetes-18: installation and use of dashboard
Error running app: default activity not found solution
asp.net core中使用Serilog以及自定义Enricher
CUDA_全局内存及访问优化
Mongodb kernel source code implementation, performance tuning, best operation and maintenance practice series command processing module source code implementation 1
Analysis steps of commodity background management
Day84: Luffy: preferential activity strategy & User Authentication & checking / settlement of shopping cart goods
恒讯科技浅谈:出现服务器宕机的处理方式