当前位置:网站首页>The length of the last word in leetcode
The length of the last word in leetcode
2020-11-09 23:48:00 【go4it】
order
This article mainly records leetcode The length of the last word
subject
Given a space that contains only uppercase and lowercase letters ' ' String s, Returns the length of its last word . If the string scrolls from left to right , So the last word is the last word .
If there is no last word , Please return 0 .
explain : A word is made up of only letters 、 Without any space characters Maximum substring .
Example :
Input : "Hello World"
Output : 5
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/length-of-last-word
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Answer key
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;
}
}
Summary
Here we traverse the string array from back to front , Cumulative length of non space encountered , In case of space, judge whether the result is 0, Not for 0 Returns the result .
doc
版权声明
本文为[go4it]所创,转载请带上原文链接,感谢
边栏推荐
- C / C + + Programming Notes: C language development tank war! In memory of our lost little overlord game
- sql 截取数据中 ‘.’的前后数据
- crm系统的成本一般是多少?
- Win7 + vs2015 + cuda10.2 configuration tensorrt7.0
- 飞鸽传书局域网找不到其他人的问题解决
- C++异常实现机制
- 函数计算进阶-IP查询工具开发
- 一幅图像能顶16x16字!——用于大规模图像缩放识别的变压器(对ICLR 2021年论文的简要回顾)
- Analysis steps of commodity background management
- Technical point 5: XML language
猜你喜欢
随机推荐
价值超10亿美元的直播系统架构图是什么样子的?
飞鸽传书局域网找不到其他人的问题解决
Interviewer: what are cache penetration, cache avalanche and cache breakdown?
Can public IP address and SSL certificate improve SEO?
2020-11-09:谈谈布隆过滤器和布谷鸟过滤器的相同点和不同点?
DB engines database ranking in November: PostgreSQL holds the top spot in the same period
表单验证,为避免全局污染,少定义全局变量写法
Win7 + vs2015 + cuda10.2 configuration tensorrt7.0
毕业即失业?大学生如何分配学习时间比例,拥有完整计算机知识体系?
获取List集合对象中某一列属性值
Come and learn! Development Guide for personalized recommendation system (with internet disk link)
IP地址SSL证书
Day84: Luffy: preferential activity strategy & User Authentication & checking / settlement of shopping cart goods
Optimization of commodity backstage system
CUDA_共享内存、访存机制、访问优化
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
白山云科技入选2020中国互联网企业百强
Hengxun Technology: the way to deal with server downtime
手把手教你使用容器服务 TKE 集群审计排查问题
DB-Engines 11月数据库排名:PostgreSQL坐稳同期涨幅榜冠军宝座