[LeetCode] Length of last word

LeetCode Length of Last Word 1. 题目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 阅读详情

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.

For example, 
Given s = "Hello World",
return 5.

这是一道实现题,从后往前扫描,如何判断最后一个word,有以下几种情况

1.“word   ”

2. "       "

3. "word"

4."  word  "

int lengthOfLastWord(const char *s) {
    int length = strlen(s);
    int count = 0;
    for(int i= length-1; i>=0;i--){
        if(s[i] == ' '){
            if(count == 0) continue;
            else return count;
        }
        count++;
    }
    return count;
}

Java解法:

利用split()函数,通过空格“ ”将原字符串分组

public int lengthOfLastWord(String s) {
        String [] splitStrings = s.split(" ");
        	for(int i=splitStrings.length-1;i>=0;i--){
        		if(splitStrings[i].length()>0)
        			return splitStrings[i].length();
        	}
        	return 0;
    }


  • public String[] split(String regex)
    Splits this string around matches of the given regular expression.

    This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

    The string "boo:and:foo", for example, yields the following results with these expressions:

    RegexResult
    :{ "boo", "and", "foo" }
    o{ "b", "", ":and:f" }
    Parameters:
    regex - the delimiting regular expression
    Returns:
    the array of strings computed by splitting this string around matches of the given regular expression
    Throws:
    PatternSyntaxException - if the regular expression's syntax is invalid

FPGA编译加速实战:6大可落地的Vivado优化策略 FPGA编译本质上是NP-hard约束满足问题求解,而非传统软件编译;Vivado作为时序驱动的硬件实现工具链,其耗时瓶颈集中在Implementation阶段的布局布线(PnR)与逻辑优化。理解这一原理,才能跳出‘加线程’‘升版本’等表面优化,转向精准时序约束、增量编译、物理锚点控制、IP配置精简等工程级干预。这些技术手段直接作用于Vivado的Tcl执行流与底层求解空间,显著压缩搜索范围、减少无效迭代——尤其适用于Zynq/UltraScale等中大型项目(LUT > 80K、多时钟域、DDR/PCIe 阅读详情

相关推荐

最邻近点对问题(Closest-Pair Problem):二维的分治解法详解

低维时,最邻近点对问题可以利用分治求解。但是网络上相关资料良莠不齐,对于3维甚至难有有价值的参考。最近恰巧梳理了123维最邻近点对问题的相关知识,故在此分享。这是该系列的第二篇,介绍二维情况的分治解法。

Carl_Rabbit的博客 1万+

LeetCode-Length of Last Word

作者:disappearedgod 文章出处:http://blog.youkuaiyun.com/disappearedgod/article/details/23621903 时间:2014-8-27

上帝已逝,自求多福 775

LT9211 单路mipi转双路mipi芯片,方案成熟,提供技术支持

LT9211 单路mipi转双路mipi芯片

TEL15012949045的博客 5014

leetcode-58. Length of Last Word最后一单词的长度 python

Length of Last Word题目python split分割遍历 题目 Given a string s consists 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 the string. If the la

。 229

腾讯云智后台开发实习面试全纪录(已offer)

腾讯云智后台开发实习面经

wenwenaier的博客 5903

对数周期天线hfss建模_Ansoft HFSS 在设计对数周期天线时的仿真方法-HFSS教程

AnsoftHFSS在设计对数周期天线时的仿真方法文章来源: ANSYS 2011中国用户大会优秀论文 录入: mweda.com 点击数:【摘要】 本文通过 ANSOFT HFSS 设计了一个对数周期天线,在仿真分析时,发现随着求解频率的不同,天线的求解结果差别较大,求解误差较大。通过在 HFSS 中尝试不同的求解设置方法, 最终通过将天线模型剖分网格最大长度限定在 1/50λ的...

weixin_42186728的博客 2098

LeetCode Algorithm 0058 - Length of Last Word (Easy)

0058 - Length of Last Word (Easy),Related Topics: String

DarkRabbit的专栏 314

告别玄学调试:用STM32CubeMX和逻辑分析仪搞定DP83848的RMII与SMI接口

本文详细介绍了如何使用STM32CubeMX和逻辑分析仪调试DP83848的RMII与SMI接口,帮助开发者超越表象,直接观察底层通信波形。通过实战案例和高级调试技巧,解决常见问题如时钟偏移、信号完整性等,提升网络接口的稳定性和可靠性。

weixin_30752699的博客 299

LeetCode——58. Length of Last Word

标题58. Length of Last Word 题目描述 给定字符串s由大写/小写字母和空格字符’'组成,返回字符串中最后一个单词的长度。 如果最后一个单词不存在,则返回0。 注意:单词定义为字符序列仅由非空格字符组成。 例子: Input: “Hello World” Output: 5 解题思路 1.将字符串转化位字符数组 2.对字符数组遍历 3.如果元素不是空格时,计数器加...

jmu_sxc的博客 169

【即插即用模块】注意力篇 | TGRS 2025 | ASD:自适应频域解耦,动态拆分高低频+空间注意力,频域空间特征双增强!

摘要: FDMF-Net提出了一种频域解耦多模态融合网络,核心组件为振幅谱解耦模块(ASD)。ASD通过自适应频域拆分和频域-空域协同增强,有效分离模态不变与模态特定特征。该模块利用MLP学习动态阈值调整高低频比例,并通过空间注意力分别强化频域特征,在遥感语义分割任务中实现了细节与结构的协同增强。实验表明,ASD模块能减少模态内冗余,提升特征判别性,为多模态数据融合提供了新思路。代码已开源,适用于需要强化细节与结构的视觉任务。

主要更新底层视觉(去噪、超分等)相关的科研内容,形式为【论文精读】+【论文复现】 306

WPF数据绑定Binding对数据的转换与效验(四)

Binding的作用就是架在Source与Target之间的桥梁,数据可以在这座桥梁的帮助下来流通。就像现实世界中的桥梁会设置一些关卡进行安检一样,Binding这座桥上也可以设置关卡对数据的有效性进行检验,不仅如此,当Binding的两端要求使用不同的数据类型时,我们还可以为数据设置转换器。Binding用于数据有效性检验的关卡是它的ValidationRules属性,用于数据类型转换的关卡是它的Converter属性。

qq_30725967的博客 1627

【Leetcode】Length of Last Word

题目链接:https://leetcode.com/problems/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 st

业精于勤,荒于嬉 1619

【系统架构设计师-2026上半年真题】综合知识-参考答案及详解(75题完全回忆版)

【系统架构设计师-2026上半年真题】综合知识-参考答案及详解(75题完全回忆版)

数据知道的博客 3101

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

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

【LeetCode with Python】 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 charact

XNERV SURVEYS 9909

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

[LeetCode] 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. Note: A word

1.02^365的成长裂变 3635

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

【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的博客 442

LeetCode——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 ...

sinat_22994595的博客 268

Length of Last Word -- LeetCode

原题链接: http://oj.leetcode.com/problems/length-of-last-word/  这道题比较简单,就是进行一下字符串操作。唯一的细节就是要去掉尾部的空格,然后读到下一个空格,记录下长度。时间复杂度是O(n),n是字符串的长度,空间复杂度是O(1)。代码如下:public int lengthOfLastWord(String s) { if(s==n

Code Ganker 8237

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

leetcode:Length of Last Word

一、 题目 给定一个字符串s由大/小写字母和空的空间字符' ',返回字符串的最后一个单词的长度。 如果最后一个单词不存在,则返回0。 注:一个单词被定义为一个不带空格的字符序列。 例如, 给出s =“Hello World”, 返回5。 二、 分析 没有什么复杂的算法,按照规则考虑所有情况就行,有下面的几种情况: 1、“   ” 2、“abc” 3、“   abc” 4、“

恰同学少年 1077

leetcode Length of Last Word

题目连接 https://leetcode.com/problems/length-of-last-word/ Length of Last Word Description Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the le...

dong12276的博客 150

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

Leetcode之Length of Last Word

题目: Given a stringsconsists 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 ...

qq_35455503的博客 147

LeetCode之Length of Last Word

http://oj.leetcode.com/problems/length-of-last-word/ 返回最后一个单词的长度,使用java的split方法即可 public class Solution { public int lengthOfLastWord(String s) { String[] arr = s.split

叠搭宝箱 913

leetcode 58. Length of Last Word

一 题目 Given a stringsconsists 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...

bohu83的博客 275
上一篇: LeetCode之动态规划 (二)
下一篇: [LeetCode30]Substring with Concatenation of All Words
Mavs
博客等级 码龄17年 166粉丝 296原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值