leetcode 58. Length of Last Word

软件工程:数据字典 数据字典 阅读详情

Given a string s consists of upper/lower-case alphabets and empty space characters ’ ', return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

Example:

Input: “Hello World”
Output: 5

给出一个string,要求return最后一个单词的长度,分隔符是space,如果没有最后一个单词,就返回0

乍一看很简单,不就是split

string[] result = s.split(" ");
return result[result.length - 1].length();

但是s=space时,result.length就会=0,然后就会ArrayIndexOutOfBounds

再者这种方法time上只超过60%

然后参照别人的一种方法,就是利用lastIndexOf(’ ')
并利用s.trim(),

但是注意s.trim()一定要保存到s,否则s是不变的
还有要注意是 s.length() -1 - index 而不是 s.length() - index,因为不包括‘ ’本身

    public int lengthOfLastWord(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        
        s = s.trim();
        
        int index = s.lastIndexOf(' ');
        
        return index == -1 ? s.length() : s.length() -1 - index;
    }
Rockchip硬件编码实战:5分钟搞定RK3588上的H.265视频压缩(附FFmpeg集成指南) 本文深入解析了在Rockchip RK3588平台上利用硬件编码器实现高效H.265视频压缩的完整实战流程。文章详细介绍了MPP库的基础使用、FFmpeg集成方法,并重点分享了零拷贝、多实例编码等关键性能优化技巧,帮助开发者充分发挥RK3588的硬件编码能力,实现从YUV原始数据到高质量H.265码流的高效转换。 阅读详情

相关推荐

CATIA二次开发CAA:(一)开发环境搭建

CATIA+CAA+RADE+VS2012

ysm1987的博客 1万+

python写算法题:leetcode: 58. Length of Last Word

class Solution(object): def lengthOfLastWord(self, s): """ :type s: str :rtype: int """ ret=0 lastch = len(s)-1 for _ in xrange(len(s)): ...

244

Camera Sensor接口协议全解析(六)聚焦onsemi HiSPi接口的技术突破与应用实践

摘要: Onsemi HiSPi是一种高速并行接口协议,专为高分辨率图像传感器设计,具备1600 Mbps传输速率、低功耗和高兼容性特点。通过对比DVP、MIPI CSI-2等主流协议,HiSPi在工业/医疗领域表现突出,支持动态功耗控制、多ROI和CRC校验。其创新架构包含多PHY设计,提供分组SP、流SP等多种工作模式,满足灵活传输需求。实际应用中,HiSPi在PCB缺陷检测(5μm精度)和液晶面板检测(0.3% Mura检出率)等场景展现卓越性能,成为高速成像领域的关键技术。

GateWorld的博客 1437

LeetCode58 Length of Last Word

详细见:leetcode.com/problems/length-of-last-word Java Solution: github package leetcode; public class P058_LengthOfLastWord { public static void main(String[] args) { System.out.println(new S

zxwtry的博客 359

[ENVI]-[高光谱遥感]-波谱沙漏分析工具的使用

图1-2-1查看特殊波段的坏波段图1-2-2在中添加坏波段ENVI波谱沙漏分析工具MNF变换。

m0_57992372的博客 1470

南京银行信息技术岗提前批笔试与面试

1、笔试 笔试大致可以分为三部分:性格测试、行测题目、专业知识 性格测试 性格测试一般会有较多的体量,可能一共大概有一百多道接近两百道的样子,这个是不限时的。 前面有100道是这样的题目,就是有四个选项都是各种形容词,然后分别选出最符合自己的与最不符合自己的选项。这种题你就顺着自己的潜意识填就行了,但是前后要一致,我怀疑他出这么多题,可能就是考一考你记不记得前面填了啥。比如你做到第90题的时候,可能会出现和前面第2题类似的四个形容词,更有甚者就是一模一样的,你要对自己之前选的什么有个比较清楚的记忆。

qq_40971025的博客 6924

【leetcode c++】58 Length of Last Word

Length of Last Word Given a string s consists ofupper/lower-case alphabets and empty space characters ' ', return the length oflast word in the string. If the last word does not exist, return 0.

lif的博客 443

Sciter教程_00_创建HelloWorld工程

本教程记录Sciter工程的创建和编译运行的过程。

qq_36707072的博客 2278

leetcode || 58、Length of Last Word

problem: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0

hustyangju的足迹 722

海康Visionmaster-全局脚本:VM 加载方案后自动执行 的方法,如何让VM在方案加载后自动执行流程?

海康Visionmaster-全局脚本:VM 加载方案后自动执行 的方法,如何让VM在方案加载后自动执行流程?

视觉人机器视觉的博客 2039

prplOS适配USB LAN网卡

本文介绍了将USB LAN网卡(如rndis0)适配到prplOS的LAN管理流程。

四儿家的小祖宗的博客 322

LeetCode58——Length of Last Word

LeetCode58——Length of Last Word 求出最后最后一个单词的长度。 提供两种方案。 方案1: 把子串按word保存在vector里面,这样求出vector最后一个元素的长度就可以了。 代码: class Solution { public: int lengthOfLastWord(string s) { auto it = s.begin(); w

上善若水 1039

HFSS实战项目——GPS北斗双模微带天线

本文设计了一款方形圆极化微带天线应用于北斗B1和GPSL1频段,满足高增益、低VSWR、适当方向图等要求。设计中心频率为1568MHz,介质板采用厚度h=1.6mm的Arlon AD450(tm)板,相对介电常数为4.5,采用探针馈电。

2401_87464239的博客 1769

[LeetCode] 58.Length of Last Word

[LeetCode] 58.Length of Last Word 题目描述 解题思路 实验代码 题目描述Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string.If the last wor

user_name_is_so_long的博客 282

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

【LeetCode】58. Length of Last Word

58. Length of Last Word Introduce Given a string s consists of upper/lower-case alphabets and empty sp...

roguesir的博客 816

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

[LeetCode]58. Length of Last Word

[LeetCode]58. Length of Last Word题目描述思路遇到空格,将计数器置为0 考虑字符串后多个空格的情况,加一个pre值,将count置0之前,检查count不为0,则将count赋给pre 最后返回,若count为0则返回pre,否则返回count代码class Solution { public: int lengthOfLastWord(string s)

Lcharon的博客 1629

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

leetcode 58 Length of Last Word C++

注意特殊情况。 int lengthOfLastWord(string s) { if(s.empty()) return 0; int right = s.size(); int left = right; while(left>0) { if(s[left-1]!=' ')

wcxdell的专栏 824

leetcode|58. Length of Last Word

问题描述:Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defi...

笔记本 369

Leetcode题解(5):L58/Length of Last Word

Leetcode题解(5):L58/Length of Last Word L58: Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return th...

weixin_30653097的博客 67

闯关leetcode——58. Length of Last Word

leetcode第58题

方亮的专栏 774

LeetCode 58. Length of Last Word

Length of Last Word - LeetCode Given a string s consists of upper/lower-case alphabets and empty space characters ’ ', return the length of last word in the string. If the last word does not exist, re...

zhaohaibo的博客 339

leetcode解题之58 # Length of Last Word Java版

leetcode解题之58 # Length of Last Word Java版

mine_song的博客 476

LeetCode String 58 Length of Last Word

58. Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space characters ’ ', return the length of last word in the string. If the last word does not exist, return 0....

Yi Jiao's Blog 183

leetcode python3 简单题58. Length of Last Word

1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第五十八题 (1)题目 英文: Given a string s consists of upper/lower-case alphabets and empty space characters ’ ', return the length of last word (last w...

从此做一个俗人 298

leetcode【58】Length of Last Word

问题描述: Given a stringsconsists of upper/lower-case alphabets and empty space characters' ', return the length of last word (last word means the last appearing word if we loop from left to right) in...

fanyuwgy的博客 121

[LeetCode 58] Length of Last Word

题目链接:length-of-last-word /** * Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does

瓦尔登湖 496
上一篇: leetcode 54. Spiral Matrix(螺旋矩阵)
下一篇: leetcode 45/55. Jump Game/ 2(跳跃游戏)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值