当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Python提示AttributeError 或者DeprecationWarning: This module was deprecated解决方法
- 没有磁盘空间 No space left on device
- [QT] subclass QObject + movetothread to realize multithreading
- 异常:Invalid or unexpected token
- LeetCode 50 Pow(x,n)
- 利用尾巴作为时间序列进行处理来识别鲸鱼
- CUDA_寄存器和局部存储器
- Come and learn! Development Guide for personalized recommendation system (with internet disk link)
- crm系统的成本一般是多少?
- 获取List集合对象中某一列属性值
猜你喜欢

Python提示AttributeError 或者DeprecationWarning: This module was deprecated解决方法

IP地址SSL证书

编码风格:Mvc模式下SSM环境,代码分层管理

做个别人家的网页

SQL intercepts the data before and after the '.'

CRM系统能帮助企业做哪些事?

“wget: 无法解析主机地址”的解决方法

SSL证书和公网IP地址如何影响SEO

Mongodb kernel source code implementation, performance tuning, best operation and maintenance practice series command processing module source code implementation 1

asp.net Using serilog in core and customizing enrich
随机推荐
一幅图像能顶16x16字!——用于大规模图像缩放识别的变压器(对ICLR 2021年论文的简要回顾)
CUDA常用概念及注意点
代码中的软件工程--对menu项目的源码分析
[QT] subclass QObject + movetothread to realize multithreading
Prometheus installation configuration
PL/SQL Developer临时用户和新手的功能指南
CUDA_常量内存
毕业设计之 ---基于微服务框架的电影院订票系统
What is the architecture of a live broadcast system worth more than $1 billion?
接缝雕刻算法:一种看似不可能的图像大小调整方法
Hand in hand to teach you to use container service tke cluster audit troubleshooting
Error running app:Default Activity not found 解决方法
November 09, 2020: talk about the similarities and differences between the bulon filter and the cuckoo filter?
JMeter的简单使用
asp.net core中使用Serilog以及自定义Enricher
js label语法跳出多重循环
What can CRM system help enterprises do?
当我们开发一个接口时需要注意些什么
crm系统的成本一般是多少?
SQL filter query duplicate columns