当前位置:网站首页>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;
}
}
边栏推荐
- Codeworks round 449 (Div. 1) C. Kodori tree template
- Dede collection plug-in does not need to write rules
- Ospfb notes - five messages [ultra detailed] [Hello message, DD message, LSR message, LSU message, lsack message]
- 软件研发的十大浪费:研发效能的另一面
- OSPF notes [multiple access, two multicast addresses with OSPF]
- LM small programmable controller software (based on CoDeSys) note 19: errors do not match the profile of the target
- Why is Hong Kong server most suitable for overseas website construction
- [recommended algorithm] C interview question of a small factory
- Use winmtr software to simply analyze, track and detect network routing
- 2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
猜你喜欢
随机推荐
做网站数据采集,怎么选择合适的服务器呢?
Maixll-Dock 使用方法
Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022
pytorch神经网络搭建 模板
Task04 mathematical statistics
2022年T电梯修理题库及模拟考试
js 图片路径转换base64格式
Summary of acl2021 information extraction related papers
如何看待智慧城市建设中的改变和机遇?
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 桥接器与交换机 / 3.4.2 多属性注册协议(Multiple Registration Protocol (MRP))
扩展-Fragment
Strategic suggestions and future development trend of global and Chinese vibration isolator market investment report 2022 Edition
TASK04|數理統計
One click shell to automatically deploy any version of redis
2022危险化学品生产单位安全生产管理人员题库及答案
1. Mobile terminal touch screen event
Some small knowledge points
How to choose the right server for website data collection?
2022 question bank and answers for safety production management personnel of hazardous chemical production units
Offline installation of Wireshark 2.6.10









