当前位置:网站首页>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; }
边栏推荐
- Day20 FPGA 】 【 - block the I2C read and write EEPROM
- 没想到MySQL还会问这些...
- 【FPGA】名词缩写
- LeetCode刷题第10天字符串系列之《125回文串验证》
- leetCode刷题14天二叉树系列之《 110 平衡二叉树判断》
- 什么是机器强化学习?原理是什么?
- 2022-08-10 The sixth group Hiding spring study notes
- [ADI low-power 2k code] Based on ADuCM4050, ADXL363, TMP75 acceleration, temperature detection and serial port printing, buzzer playing music (lone warrior)
- Audio codec, using FAAC to implement AAC encoding
- MYSQLg高级------回表
猜你喜欢
Graphical LeetCode - 640. Solving Equations (Difficulty: Moderate)
云平台下ESB产品开发步骤说明
Design and Realization of Employment Management System in Colleges and Universities
Description of ESB product development steps under cloud platform
LeetCode刷题第12天二叉树系列之《104 二叉树的最大深度》
电力机柜数据监测RTU
互换性与测量技术-公差原则与选用方法
Qnet Weak Network Test Tool Operation Guide
移动端地图开发选择哪家?
leetcode刷题第13天二叉树系列之《98 BST及其验证》
随机推荐
【FPGA】day22-SPI协议回环
En-us is an invalid culture error solution when Docker links sqlserver
【FPGA】day22-SPI protocol loopback
Will oracle cardinality affect query speed?
互换性测量技术-几何误差
The impact of programmatic trading and subjective trading on the profit curve!
常见布局效果实现方案
LeetCode刷题第10天字符串系列之《125回文串验证》
【FPGA】day20-I2C读写EEPROM
LeetCode刷题第11天字符串系列之《 58最后一个单词长度》
MYSQLg高级------回表
【FPGA】abbreviation
[FPGA] day19- binary to decimal (BCD code)
Is there any way for kingbaseES to not read the system view under sys_catalog by default?
The last update time of the tables queried by the two nodes of the rac standby database is inconsistent
我的 archinstall 使用手册
leetCode刷题14天二叉树系列之《 110 平衡二叉树判断》
Homework 8.10 TFTP protocol download function
Multi-serial port RS485 industrial gateway BL110
2022-08-10 第六小组 瞒春 学习笔记