当前位置:网站首页>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;
}
}
边栏推荐
- LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机
- 1. Mobile terminal touch screen event
- 尺取法:有效三角形的个数
- AssertionError assert I.ndim == 4 and I.shape[1] == 3
- Summary of acl2021 information extraction related papers
- Registration for R2 mobile pressure vessel filling test in 2022 and R2 mobile pressure vessel filling free test questions
- 为什么香港服务器最适合海外建站使用
- 2022 hoisting machinery command registration examination and hoisting machinery command examination registration
- The index is invalid
- What is uid? What is auth? What is a verifier?
猜你喜欢

2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank

TCP server communication flow

pytorch 卷积操作

Section 27 remote access virtual private network workflow and experimental demonstration

【硬十宝典】——1.【基础知识】电源的分类

Pytorch(一) —— 基本语法

Offline installation of Wireshark 2.6.10

MySQL winter vacation self-study 2022 12 (5)

神经网络-卷积层

How to do the performance pressure test of "Health Code"
随机推荐
OdeInt与GPU
Basic exercise of test questions hexadecimal to decimal
MySQL advanced -- you will have a new understanding of MySQL
做网站数据采集,怎么选择合适的服务器呢?
Common interview questions ①
CF1638E colorful operations
VIM简易使用教程
【硬十宝典】——2.【基础知识】开关电源各种拓扑结构的特点
Day 52 - tree problem
JS rotation chart
Codeforces Round #721 (Div. 2)B1. Palindrome Game (easy version)B2. Palindrome game (hard version)
Daily question - line 10
2022年聚合工艺考试题及模拟考试
Task04 mathematical statistics
软件研发的十大浪费:研发效能的另一面
2022年上海市安全员C证考试题模拟考试题库及答案
RuntimeError: “max_pool2d“ not implemented for ‘Long‘
Difference between cookie and session
Section 27 remote access virtual private network workflow and experimental demonstration
2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination