当前位置:网站首页>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 implement LRU algorithm
- SQL intercepts the data before and after the '.'
- 将Map中对应的key和value赋值到对象中
- Day84: Luffy: preferential activity strategy & User Authentication & checking / settlement of shopping cart goods
- The problem of looting by leetcode
- 技术点5:XML语言
- Python中[:]与[::]的用法
- 探访2020 PG技术大会
- 必看!RDS 数据库入门一本通(附网盘链接)
- The number of more than half of the array is printed by the sword
猜你喜欢

价值超10亿美元的直播系统架构图是什么样子的?

ES6, ES7, es8 Learning Guide

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

How much is the cost of CRM system?

Important components of Apache Hadoop

Make a home page

Can public IP address and SSL certificate improve SEO?

lvgl 库 V7版本相关应用

LinkedList源码简析

Python prompt attributeerror or depreciation warning: This module was degraded solution
随机推荐
SQL filter query duplicate columns
商品后台系统实现
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
异常:Invalid or unexpected token
Technical point 5: XML language
CUDA_主机内存
Nodejs: handwritten koa Middleware
C / C + + Programming Notes: C language development tank war! In memory of our lost little overlord game
sql 截取数据中 ‘.’的前后数据
asp.net Using serilog in core and customizing enrich
Python调用飞书发送消息
What is the architecture of a live broadcast system worth more than $1 billion?
Baishan cloud technology is selected as the top 100 Internet enterprises in China in 2020
Aikang Guobin denounced Guoxin Securities report as untrue and sent a lawyer's letter
CUDA_ Register and local memory
Operation and design of rights management in ERP
编码风格:Mvc模式下SSM环境,代码分层管理
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:王健
爱康国宾怒斥国信证券报告失实,已发律师函
How SSL certificate and public IP address affect SEO