当前位置:网站首页>【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
边栏推荐
猜你喜欢

Yangtao electronic STM32 Internet of things entry 30 step notes II. Cube ide download, installation, sinicization and setting

vmware 搭建ES8的常见错误

Idea: export Yapi interface using easyyapi plug-in

洋桃电子STM32物联网入门30步笔记一、HAL库和标准库的区别

Asan minimalism

Green apple film and television system source code film and television aggregation film and television navigation film and television on demand website source code

QT compilation qtxlsx Library

Noyer électronique stm32 Introduction à l'Internet des objets 30 étapes notes I. différences entre la Bibliothèque Hal et la Bibliothèque standard

Notes on 30 steps of introduction to the Internet of things of yangtao electronics STM32 III. cubemx graphical programming and setting the IO port on the development board

Transformer XL: attention language modelsbbeyond a fixed length context paper summary
随机推荐
分布式消息中间件框架选型-数字化架构设计(7)
Redis Desktop Manager for Mac(Redis可视化工具)
input元素添加监听事件
Anonymous type (c Guide Basics)
The annotation is self-defined by implementing the parameter parser handlermethodargumentresolver interface
A simple theme of Typecho with beautiful appearance_ Scarfskin source code download
根据字节码获取类的绝对路径
测试你的机器学习流水线
tsdf +mvs
Yangtao electronic STM32 Internet of things entry 30 step notes IV. engineering compilation and download
QT compilation qtxlsx Library
Using qlst excel file
Community group purchase applet source code + interface DIY + nearby leader + supplier + group collage + recipe + second kill + pre-sale + distribution + live broadcast
《深度学习》学习笔记(八)
pgsql想实现mysql一样样的列子查询操作
396. Rotate Function
关于数组复制问题
Record: JS several methods to delete one or more items in the array
RPC procedure
STM32 uses Hal library. The overall structure and function principle are introduced