当前位置:网站首页>Leetcode58. 最后一个单词的长度
Leetcode58. 最后一个单词的长度
2022-07-28 03:43:00 【Java全栈研发大联盟】
题目传送地址: https://leetcode.cn/problems/length-of-last-word/
运行效率:
代码如下:
public static int lengthOfLastWord(String s) {
//处理边界条件
if ("".equals(s.trim())) {
//对于" "这种需要trim()
return 0;
}
if (s.length() == 1) {
return 1;
}
int lastWordEnd = s.length() - 1;
//先找到最后一个单词的尾部
while (lastWordEnd != -1 && s.charAt(lastWordEnd) == ' ') {
lastWordEnd--;
}
int lastWordStart = lastWordEnd;
//再找到最后一个单词的首字母
while (lastWordStart!=-1&&s.charAt(lastWordStart) != ' ') {
lastWordStart--;
}
//因为subtsring(start,end)函数截取的字符串不包含end位置的字符,所以下面要+1
String substring = s.substring(lastWordStart + 1, lastWordEnd + 1);
return substring.length();
}
边栏推荐
- Differences among BRD, MRD and PRD
- 某宝模拟登录,减少二次验证的方法
- [paper notes] mobile robot autonomous navigation experimental platform based on deep learning
- ES6 from getting started to mastering 08: extended object functions
- [openvx] VX for basic use of objects_ lut
- Redis source code analysis (who says C language can't analyze it?)
- Weekly recommended short video: how to correctly understand the word "lean"?
- Billions of asset addresses are blacklisted? How to use the tether address freezing function?
- 每周推荐短视频:如何正确理解“精益”这个词?
- Input upload file and echo FileReader and restrict the type of file selection
猜你喜欢

C语言:求一个整数存储在内存中的二进制中1的个数

Implementation of online rental system based on SSM

过亿资产地址被拉入黑名单?Tether地址冻结功能该怎么用?

TypeError: ufunc ‘bitwise_and‘ not supported for the input types, and the inputs could not be safely

ES6 从入门到精通 # 09:Symbol 类型

Msgan is used for pattern search of multiple image synthesis to generate confrontation Network -- to solve the problem of pattern collapse

Integrate SSM to realize search of addition, deletion, modification and query

Unity backpack system

ES6 from getting started to mastering 08: extended object functions

The latest version of pagoda installs the zip extension, and PHP -m does not display the processing method
随机推荐
Differences among BRD, MRD and PRD
动态规划——1049. 最后一块石头的重量 II
verticle-align行内元素垂直居中对齐
Redis source code analysis (who says C language can't analyze it?)
"Xiaodeng" network equipment monitoring in operation and maintenance
动态规划——63. 不同路径 II
AI chief architect 12 AICA Baidu OCR vertical large-scale landing practice
[openvx] VX for basic use of objects_ lut
【OPENVX】对象基本使用之vx_pyramid
Tungsten Fabric SDN — BGP as a Service
[openvx] VX for basic use of objects_ distribution
【P4】 查看库文件两个历史版本的区别
玩一玩WolframAlpha计算知识引擎
Billions of asset addresses are blacklisted? How to use the tether address freezing function?
Data mining-02
D2dengine edible tutorial (4) -- draw text
leetcode刷题:动态规划09(最后一块石头的重量 II)
How to make the Internet access the intranet IP (used by esp8266 web pages)
20220727 use the Bluetooth module hc-05 of Huicheng technology to pair mobile phones for Bluetooth serial port demonstration
构建“产业大脑”,以“数字化”提升园区运营管理及服务能力!