当前位置:网站首页>LeetCode_58(最后一个单词的长度)
LeetCode_58(最后一个单词的长度)
2022-07-01 04:37:00 【***】
题目描述:
给你一个字符串 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 中至少存在一个单词
class Solution {
public int lengthOfLastWord(String s) {
int res=0;
for (int i = s.length()-1; i >=0; i--) {
int num=(int)s.charAt(i);
if((num>122||num<65||(num>90&&num<97))&&res==0)continue;
else if((num>122||num<65||(num>90&&num<97))&&res!=0)break;
else if ((num>=97&&num<=122)||(num>=65&&num<=90)) res++;
}
return res;
}
}
边栏推荐
- Pytorch(二) —— 激活函数、损失函数及其梯度
- 总结全了,低代码还需要解决这4点问题
- How to choose the right server for website data collection?
- All in all, the low code still needs to solve these four problems
- Leetcode learning - day 36
- selenium打开chrome浏览器时弹出设置页面:Mircrosoft Defender 防病毒要重置您的设置
- 2022年化工自动化控制仪表操作证考试题库及答案
- Basic exercise of test questions hexadecimal to decimal
- MySQL winter vacation self-study 2022 12 (5)
- pytorch 卷积操作
猜你喜欢
Section 27 remote access virtual private network workflow and experimental demonstration
js 图片路径转换base64格式
Pytest automated testing - compare robotframework framework
2022 polymerization process test questions and simulation test
Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)
Threejs opening
LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机
OdeInt与GPU
2022.2.7-2.13 AI industry weekly (issue 84): family responsibilities
The index is invalid
随机推荐
[godot] unity's animator is different from Godot's animplayer
The index is invalid
Daily question - line 10
Research on medical knowledge atlas question answering system (I)
2022年上海市安全员C证考试题模拟考试题库及答案
数据加载及预处理
(12) Somersault cloud case (navigation bar highlights follow)
1. Mobile terminal touch screen event
Leecode records the number of good segmentation of 1525 strings
Learn Chapter 20 of vue3 (keep alive cache component)
Talk about testdeploy
Summary of acl2021 information extraction related papers
CUDA development and debugging tool
Basic exercise of test questions hexadecimal to decimal
Shell之分析服务器日志命令集锦
Task04 mathematical statistics
Announcement on the list of Guangdong famous high-tech products to be selected in 2021
先有网络模型的使用及修改
Leetcode learning - day 36
Grey correlation cases and codes