当前位置:网站首页>Leetcode58. Length of the last word
Leetcode58. Length of the last word
2022-07-28 03:47:00 【Java full stack R & D Alliance】
Title transmission address : https://leetcode.cn/problems/length-of-last-word/
Operational efficiency :
The code is as follows :
public static int lengthOfLastWord(String s) {
// Dealing with boundary conditions
if ("".equals(s.trim())) {
// about " " This need trim()
return 0;
}
if (s.length() == 1) {
return 1;
}
int lastWordEnd = s.length() - 1;
// Find the end of the last word first
while (lastWordEnd != -1 && s.charAt(lastWordEnd) == ' ') {
lastWordEnd--;
}
int lastWordStart = lastWordEnd;
// Find the first letter of the last word
while (lastWordStart!=-1&&s.charAt(lastWordStart) != ' ') {
lastWordStart--;
}
// because subtsring(start,end) The string intercepted by the function does not contain end The character of position , So next +1
String substring = s.substring(lastWordStart + 1, lastWordEnd + 1);
return substring.length();
}
边栏推荐
- leetcode刷题:动态规划08(分割等和子集)
- BRD,MRD,PRD的区别
- 【LeetCode】34、在排序数组中查找元素的第一个和最后一个位置
- CH340 RTS DTR引脚编程驱动OLED
- Data mining-02
- LightPicture – 精致图床系统
- Airiot Q & A issue 6 | how to use the secondary development engine?
- Prefix-Tuning: Optimizing Continuous Prompts for Generation
- 常用的弱网测试工具
- The wonderful use of asemi rectifier bridge GBPC3510 in DC brush motor
猜你喜欢

After 95, Alibaba P7 published the payroll: it's really heartbreaking

What is tor? What is the use of tor browser update?

conda虚拟环境总结与解读

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

Interface automation test, complete introduction

BRD,MRD,PRD的区别

Light year admin background management system template

Vertical align align the elements in the row are vertically centered
D2dengine edible tutorial (4) -- draw text

Qt:qmessagebox message box, custom signal and slot
随机推荐
Weekly recommended short video: how to correctly understand the word "lean"?
【原型与原型链】初识原型与原型链~
LeetCode_ 409_ Longest palindrome string
Unity backpack system
test case management tool
服务器内存故障预测居然可以这样做!
In depth introduction to sap ui5 fileuploader control - why do you need a hidden iframe trial
数据挖掘-02
Dynamic planning - 1049. Weight of the last stone II
构建“产业大脑”,以“数字化”提升园区运营管理及服务能力!
Leetcode58. 最后一个单词的长度
Input upload file and echo FileReader and restrict the type of file selection
【OPENVX】对象基本使用之vx_lut
【OPENVX】对象基本使用之vx_matrix
[P4] solve the conflict between local file modification and library file
数据挖掘-01
递归和非递归分别实现求第n个斐波那契数
Dynamic planning - 62. Different paths
Integrate SSM to realize search of addition, deletion, modification and query
ES6 from getting started to mastering 08: extended object functions