当前位置:网站首页>【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 <= 104
s
仅有英文字母和空格' '
组成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
边栏推荐
- 记录:js删除数组中某一项或几项的几种方法
- Misunderstanding of flush () method of OutputStream class
- 洋桃电子STM32物联网入门30步笔记三、新建CubeIDE工程和设置讲解
- 第一性原理 思维导图
- 信息收集相关知识点及题解
- tsdf +mvs
- The annotation is self-defined by implementing the parameter parser handlermethodargumentresolver interface
- RPC过程
- Community group purchase applet source code + interface DIY + nearby leader + supplier + group collage + recipe + second kill + pre-sale + distribution + live broadcast
- 如何保护开源项目免遭供应链攻击-安全设计(1)
猜你喜欢
虚拟线上展会-线上vr展馆实现24h沉浸式看展
stm32以及freertos 堆栈解析
Excle plus watermark
Search the complete navigation program source code
Ansible Automation Operation and Maintenance details (ⅰ) Installation and Deployment, Parameter use, list Management, Profile Parameters and user level ansible operating environment Construction
项目上传部分
洋桃电子STM32物联网入门30步笔记二、CubeIDE下载、安装、汉化、设置
SYS_CONNECT_BY_PATH(column,'char') 结合 start with ... connect by prior
ansible自動化運維詳解(一)ansible的安裝部署、參數使用、清單管理、配置文件參數及用戶級ansible操作環境構建
一个必看的微信小程序开发指南1-基础知识了解
随机推荐
lgb,xgb,cat k折交叉验证
stm32以及freertos 堆栈解析
怎样读取Excel表格到数据库
ansible自動化運維詳解(一)ansible的安裝部署、參數使用、清單管理、配置文件參數及用戶級ansible操作環境構建
'恶霸' Oracle 又放大招,各大企业连夜删除 JDK。。。
RPC过程
HAL库的RCC简介
Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
Word plus watermark
Noyer électronique stm32 Introduction à l'Internet des objets 30 étapes notes I. différences entre la Bibliothèque Hal et la Bibliothèque standard
Let the earth have less "carbon" and rest on the road
DOM 学习之—添加+-按钮
Goland 调试go使用-大白记录
监控智能回放是什么,如何使用智能回放查询录像
Shell script advanced
jsp页面编码
QT reading and writing XML files
作文以记之 ~ 二叉树的后序遍历
Add listening event to input element
vmware 搭建ES8的常见错误