当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
猜你喜欢
How to greatly improve the performance of larravel framework under php7? Install stone!
SRM系统是什么系统?SRM供应商管理系统功能
Dongge ate grapes when he ate an algorithm problem!
快来学习!个性化推荐系统开发指南(附网盘链接)
Usage of [:] and [::] in Python
京淘项目day10
How to make a set of K reverse linked lists
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
在PHP7下怎么大幅度提升Laravel框架性能?安装Stone!
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
随机推荐
获取List集合对象中某一列属性值
获取List集合对象中某一列属性值
Python提示AttributeError 或者DeprecationWarning: This module was deprecated解决方法
CUDA_常量内存
公网IP地址和SSL证书可以提升SEO吗?
11.9
Python调用飞书发送消息
How to implement LRU algorithm
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
LeetCode 50 Pow(x,n)
How to carry out modular power operation efficiently
C++异常实现机制
商品后台管理实现分析步骤
白山云科技入选2020中国互联网企业百强
异常:Invalid or unexpected token
C/C++编程笔记:C语言开发坦克大战!纪念我们逝去的小霸王游戏
爱康国宾怒斥国信证券报告失实,已发律师函
JT Jingtao project
win7+vs2015+cuda10.2配置TensorRT7.0
Interviewer: what are cache penetration, cache avalanche and cache breakdown?