当前位置:网站首页>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; }

边栏推荐
- C language recv() function, recvfrom() function, recvmsg() function
- 【FPGA】SDRAM
- typedef defines the structure array type
- Echart地图的省级,以及所有地市级下载与使用
- Leetcode 669. 修剪二叉搜索树
- The "top pillar" slides, and new growth is extremely difficult to shoulder the heavy responsibility. Is Ali "squatting" to jump higher?
- uni-app - 获取汉字拼音首字母(根据中文获取拼音首字母)
- uni-app - city selection index list / city list sorted by A-Z (uview component library IndexList index list)
- Audio codec, using FAAC to implement AAC encoding
- The solution to the height collapse problem
猜你喜欢
随机推荐
App基本框架搭建丨日志管理 - KLog
程序化交易的策略类型可以分为哪几种?
Detailed explanation of VIT source code
Graphical LeetCode - 640. Solving Equations (Difficulty: Moderate)
Build Zabbix Kubernetes cluster monitoring platform
How can users overcome emotional issues in programmatic trading?
电商项目——商城限时秒杀功能系统
机器学习可以应用在哪些场景?机器学习有什么用?
【FPGA】day18-ds18b20实现温度采集
Element's BFC attribute
【FPGA】设计思路——I2C协议
What kind of programming trading strategy types can be divided into?
QueryDet: Cascading Sparse Query Accelerates Small Object Detection at High Resolution
The custom of the C language types -- -- -- -- -- - structure
Will oracle cardinality affect query speed?
LeetCode刷题第17天之《3 无重复字符的最长子串》
移动端地图开发选择哪家?
E-commerce project - mall time-limited seckill function system
js 将字符串作为js执行代码使用
The solution to the height collapse problem




![[FPGA] day19- binary to decimal (BCD code)](/img/d8/6d223e5e81786335a143f135385b08.png)



