当前位置:网站首页>【58】最后一个单词的长度【LeetCode】
【58】最后一个单词的长度【LeetCode】
2022-04-23 08:18:00 【书启秋枫】
1.题目描述
给你一个字符串
s,由若干单词组成,单词前后用一些空格字符隔开。返回字符串中 最后一个 单词的长度。单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。
示例 1:
输入:s = "Hello World" 输出:5 解释:最后一个单词是“World”,长度为5。示例 2:
输入:s = " fly me to the moon " 输出:4 解释:最后一个单词是“moon”,长度为4。示例 3:
输入:s = "luffy is still joyboy" 输出:6 解释:最后一个单词是长度为6的“joyboy”。提示:
1 <= s.length <= 104s仅有英文字母和空格' '组成s中至少存在一个单词
2.核心代码
class Solution {
public int lengthOfLastWord(String s) {
String[] split = s.split(" ");
return split[split.length - 1].length();
}
}
3.测试代码
class Solution {
public int lengthOfLastWord(String s) {
String[] split = s.split(" ");
return split[split.length - 1].length();
}
}
// @solution-sync:end
class Main {
public static void main(String[] args) {
String s = "Hello World";
int result = new Solution().lengthOfLastWord(s);
System.out.println(result);
}
}
版权声明
本文为[书启秋枫]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_45037155/article/details/124356565
边栏推荐
猜你喜欢

STM32使用HAL库,整体结构和函数原理介绍

Shell脚本进阶

RCC introduction of Hal Library

ATSS(CVPR2020)

How to generate assembly file

Using qlst excel file

如何保护开源项目免遭供应链攻击-安全设计(1)

DJ音乐管理软件Pioneer DJ rekordbox

Let the earth have less "carbon" and rest on the road

'bully' Oracle enlarged its move again, and major enterprises deleted JDK overnight...
随机推荐
Qtablewidget header customization and beautification developed by pyqt5 (with source code download)
How browser works
微信小程序 catchtap=“toDetail“ 事件问题
'bully' Oracle enlarged its move again, and major enterprises deleted JDK overnight...
DJ音乐管理软件Pioneer DJ rekordbox
rembg 分割mask
What is RPC
DOM learning notes - traverse all element nodes of the page
SYS_CONNECT_BY_PATH(column,'char') 结合 start with ... connect by prior
excle加水印
Type anonyme (Principes fondamentaux du Guide c)
Redis master-slave server problem
Redis Desktop Manager for Mac(Redis可视化工具)
synchronized 实现原理
Stm32f103zet6 [development of standard library functions] - Introduction to library functions
MySQL数据库中delete、truncate、drop原理详解
测试你的机器学习流水线
How to generate assembly file
根据字节码获取类的绝对路径
二维01背包