[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

ios开发拒绝“玄学”Crash:从循环引用到脏内存,构建你的系统性排查思维模型 你申请了内存,写了数据,这块内存就是脏的。系统无法直接回收它,除非你显式释放,或者系统把它 swap 出去(但在 iOS 上,Compressed Memory 机制虽然能压缩,但不会像桌面系统那样频繁 swap 到磁盘,一旦压缩内存也撑不住,你也得挂)。所以,排查 OOM 的核心心法只有一条:不要问“哪里漏了”,要问“每一刻,留在这个页面上的内存,凭什么必须在这里?一旦内存水位线逼近阈值(不同机型不同,老机器可能 600MB 就危,新机器能抗 1.5GB),系统发个警告。 阅读详情

相关推荐

零基础转行AI产品经理:从技术小白到实战入行的完整路径

人工智能技术正在重塑互联网产品岗位的职责边界。AI产品经理并不等同于“会用AI工具”,而是需要理解模型能力边界、掌握技术对话能力,并能将模糊需求转化为可落地的产品方案。大模型、RAG检索增强、Agent智能体等概念构成了当前AI应用的核心技术栈,理解它们的原理与适用场景,是评估需求可行性、设计容错机制的基础。从智能客服到知识库问答,这些典型应用场景背后都离不开对技术方案选型与数据质量的判断。对于非技术背景的转行者而言,真正的门槛并非算法公式,而是能否建立系统认知、动手做出Demo,并具备产品基本功。本文基于

weixin_30641597的博客 289

LeetCode-Length of Last Word

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

上帝已逝,自求多福 775

零、HarmonyOS应用开发者基础学习总览

零、HarmonyOS应用开发者基础学习总览

weixin_45029825的博客 1459

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

【OpenCV3】cv::compare()使用详解

cv::compare()主要用于两个图像之间进行逐像素的比较,并输出比较的结果。具体用法如下: cv::compare() bool cv::compare( cv::InputArray src1, // 输入数组1 cv::InputArray src2, // 输入数组2 cv::OutputArray dst, // 输出数组 int cmpop // 比较操作子,见下表 ...

guduruyu的专栏 9776

Sylvester结式法求解多项式方程

Sylvester结式法,结式,消元法,ctfshow 七夕杯 LUCK7,crypto,WriteUp

u010883831的博客 4552

LeetCode Algorithm 0058 - Length of Last Word (Easy)

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

DarkRabbit的专栏 315

安卓直链APK安装拦截全解析:从Play Protect到厂商管控的应对指南

在移动应用分发领域,APK(Android Package)安装是开发者与用户交互的关键环节。其核心原理涉及安卓系统的安全沙箱机制与权限管理体系,旨在保护用户设备免受恶意软件侵害。这一机制的技术价值在于平衡了应用生态的开放性与安全性,但也在应用推广、企业内部分发等场景中带来了挑战。尤其在通过官网、广告等直链渠道分发APK时,常会遇到安装转化率骤降的问题。这背后主要受到Google Play Protect云端扫描、安卓系统安装权限模型演进(如Android 8.0+的安装源授权)以及设备制造商附加安全层(如

weixin_33894640的博客 481

LeetCode——58. Length of Last Word

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

jmu_sxc的博客 170

ComfyUI-controlnet控制出图

提供加载预处理器节点。

Bu2n的博客 5511

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

【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

业精于勤,荒于嬉 1622

温度PID控制PLC程序

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

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

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

【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 9910

[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

【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

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 8238

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、“

恰同学少年 1079

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的博客 151

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的足迹 723

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、付费专栏及课程。

余额充值