当前位置:网站首页>LeetCode Brush Questions Day 11 String Series "58 Last Word Length"
LeetCode Brush Questions Day 11 String Series "58 Last Word Length"
2022-08-11 03:51:00 【small machine double】
LeetCode 58最后一个单词长度
题目描述
给定一个仅包含大小写字母和空格 ’ ’ 的字符串 s,返回其最后一个单词的长度.如果字符串从左向右滚动显示,那么最后一个单词就是最后出现的单词.
如果不存在最后一个单词,请返回 0 .
说明:一个单词是指仅由字母组成、不包含任何空格字符的 最大子字符串.
示例
输入: “Hello World”
输出: 5
题目解析
按照题目的意思,Anyway, to find the length of the last word with letters(Of course it's not up to us whether the spelling is correct or not).Then start traversing from back to front,统计字符的个数,When a space is encountered, the length of the statistics can be returned.But the string given by the title may give multiple spaces at the end,例如:“hello world ”.In this case, just remove the leading and trailing spaces.There are two ways to remove it:
- 使用java字符串中的trim方法去掉首尾空格
- don't show,Skip the last space directly when counting,Counting begins when the first character that is not a space is encountered.
下面是两种实现方式:
对于java,C++,pythonsuch language,There are slice operations for strings,即split.The following is used directlysplit函数,Divide the input string into a string array based on spaces,Return the length of the last string in the array.
public int lengthOfLastWord(String s) { String[] split = s.split(" "); if (split.length == 0) { //sIf all are spaces, the array after splitting is empty return 0; } return split[split.length - 1].length(); }程序执行结果:

Use the abovesplitThe method is simple and straightforward,But not so fast,The method of counting directly from the back to the front is much faster,代码如下:
public int lengthOfLastWord(String s) { int count = 0; //s = s.trim(); //Remove leading and trailing spaces in advance for (int i = s.length() - 1; i >= 0; i--) { if (s.charAt(i) == ' ' && count == 0) { //Just skip the trailing spaces,没有用trimmethod can do so continue; } if (s.charAt(i) == ' ') { //After skipping all the spaces at the end, if there is a space after it, it proves that the length of the last word has been counted break; } count++; } return count; }

边栏推荐
- Roewe imax8ev cube battery security, what blackening and swelling are hidden behind it?
- UNI-APP_iphone苹果手机底部安全区域
- 2022-08-10 The sixth group Hiding spring study notes
- A simple JVM tuning, learn to write it on your resume
- pathman_config、pathman_config_params 删除后,如何重建?
- 什么是三方支付?
- Docker 链接sqlserver时出现en-us is an invalid culture错误解决方案
- Differences and connections between distributed and clustered
- 什么是机器强化学习?原理是什么?
- es-head插件插入查询以及条件查询(五)
猜你喜欢
随机推荐
How to delete statements audit log?
多商户商城系统功能拆解26讲-平台端分销设置
LeetCode刷题第10天字符串系列之《125回文串验证》
When EasyCVR is connected to the GB28181 device, what is the reason that the device is connected normally but the video cannot be played?
uni-app - 城市选择索引列表 / 通过 A-Z 排序的城市列表(uview 组件库 IndexList 索引列表)
程序化交易改变了什么?
论文精度 —— 2017 CVPR《High-Resolution Image Inpainting using Multi-Scale Neural Patch Synthesis》
高度塌陷问题的解决办法
Day20 FPGA 】 【 - block the I2C read and write EEPROM
Build Zabbix Kubernetes cluster monitoring platform
获取链表长度
KingbaseES有什么办法,默认不读取sys_catalog下的系统视图?
Design and Realization of Employment Management System in Colleges and Universities
MYSQLg高级------聚簇索引和非聚簇索引
EasyCVR接入GB28181设备时,设备接入正常但视频无法播放是什么原因?
【FPGA】SDRAM
【FPGA】day21-移动平均滤波器
图解LeetCode——640. 求解方程(难度:中等)
How to rebuild after pathman_config and pathman_config_params are deleted?
MYSQLg advanced ------ return table









