当前位置:网站首页>【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
边栏推荐
- 匿名類型(C# 指南 基礎知識)
- 面了一圈,整理了这套面试题。。
- Transformer XL: attention language modelsbbeyond a fixed length context paper summary
- Sword finger offer Day24 math (medium)
- vmware 搭建ES8的常见错误
- Misunderstanding of flush () method of OutputStream class
- 耳穴诊疗随笔0421
- Ansible Automation Operation and Maintenance details (ⅰ) Installation and Deployment, Parameter use, list Management, Profile Parameters and user level ansible operating environment Construction
- 记录:js删除数组中某一项或几项的几种方法
- ATSS(CVPR2020)
猜你喜欢

洋桃電子STM32物聯網入門30步筆記一、HAL庫和標准庫的區別

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

The annotation is self-defined by implementing the parameter parser handlermethodargumentresolver interface

测试你的机器学习流水线

CGM optimizes blood glucose monitoring and management -- Yiyu technology appears in Sichuan International Medical Exchange Promotion Association

Reference passing 1

Failed to convert a NumPy array to a Tensor(Unsupported Object type int)

vmware 搭建ES8的常见错误

ATSS(CVPR2020)
![[C语言] 文件操作《一》](/img/89/b19dda13d27e37fedf6736c102245b.png)
[C语言] 文件操作《一》
随机推荐
Trust uses Tokio's notify and timeout to achieve the effect similar to the timeout condition variable
How browser works
如何保护开源项目免遭供应链攻击-安全设计(1)
【精品】利用动态代理实现事务统一管理 二
ajax防止缓存方法
Protobuf简介
Add listening event to input element
Detailed description of self feeling of auricular point weight loss 0422
Shell脚本进阶
ATSS(CVPR2020)
获取TrustedInstaller权限
form中enctype属性
Yangtao electronic STM32 Internet of things entry 30 step notes II. Cube ide download, installation, sinicization and setting
匿名类型(C# 指南 基础知识)
Search the complete navigation program source code
npm安装yarn
Type anonyme (Principes fondamentaux du Guide c)
tsdf +mvs
[C语言] 文件操作《一》
Kubernetes如何使用harbor拉去私有镜像