首页 > 其他分享 >[LeetCode]Length of Last Word

[LeetCode]Length of Last Word

时间:2023-02-02 15:37:57浏览次数:30  
标签:return int end start Length Word world Last size


Question
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​​.


本题难度Easy。

【复杂度】
时间 O(N) 空间 O(1)

【思路】
只要碰到单词就记录其​​​start​​​与​​end​​​即可。分别对以下几种情况进行讨论:
一、判断start
1、单词顶格,例如​​​"world"​​​,当遍历到​​w​​​时要判断​​i==0​​​
2、单词不顶格,例如​​​" world"​​​或​​"hello world"​​​中的​​world​​​,当遍历到​​w​​​时要判断前一个字符是否为空格。
二、判断end
1、单词顶格,例如​​​"world"​​​,当遍历到​​d​​​时要判断​​i==size-1​​​
2、单词不顶格,例如​​​"world "​​,当遍历到空格(第5个)时要判断前一个字符是否不为空格

【代码】

public class Solution {
public int lengthOfLastWord(String s) {
//require
if(s==null)
return 0;
int size=s.length();
if(size<1)
return 0;
//invariant
int start=-1,end=-1;
for(int i=0;i<size;i++){
char c=s.charAt(i);
if(c!=' '){
if(i==0)
start=i;
else if(s.charAt(i-1)==' ')
start=i;
if(i==size-1)
end=i;
}else{
if(i!=0&&s.charAt(i-1)!=' ')
end=i-1;
}
}
//ensure
return (end==-1)?0:end-start+1;

}
}


标签:return,int,end,start,Length,Word,world,Last,size
From: https://blog.51cto.com/u_9208248/6033685

相关文章

  • 报错:Unexpected reserved word 'await'?
    async和await是成对出现的。会报上边的错是因为没有把async放在和await最近的函数上。举例:报错代码:1asyncconfirmAll(){2this.$refs['editeForm'].valida......
  • elasticsearch-8.6.0 配置文件
    #========================ElasticsearchConfiguration=========================##NOTE:Elasticsearchcomeswithreasonabledefaultsformostsettings.#......
  • PHPMyWind支持Word一键粘贴
    ​ 百度ueditor新增的将word内容导入到富文本编辑框的功能怎么没有啊,...ueditor实现word文档的导入和下载功能的方法:1、UEditor没有提供word的导入功能,只能说是粘贴复......
  • PHPMyWind支持Word粘贴
    ​ 图片的复制无非有两种方法,一种是图片直接上传到服务器,另外一种转换成二进制流的base64码目前限chrome浏览器使用首先以um-editor的二进制流保存为例:打开umeditor.j......
  • 写了个监控 ElasticSearch 进程异常的脚本!
    作者:JackTian来源:公众号「杰哥的IT之旅」ID:Jake_Internet服务器配置免密钥环境准备:配置免密钥前,需要在服务器的hosts文件中配置目标主机名称与IP对应关系。vim/......
  • gitk 报错 X Error of failed request: BadLength (poly request too large or intern
    问题今天修改了一下hugo的配置文件,然后使用gitk来查看改动,结果报错"XErroroffailedrequest:BadLength(polyrequesttoolargeorinternalXliblengtherror......
  • ELK系列(5) - Logstash怎么分割字符串并添加新的字段到Elasticsearch
    问题有时候我们想要在Logstash里对收集到的日志等信息进行分割,并且将分割后的字符作为新的字符来index到Elasticsearch里。假定需求如下:Logstash收集到的日志字段​​messag......
  • ELK系列(4) - Elasticsearch cannot write xcontent for unknown value of type class
    问题与分析在使用Elasticsearch进行index数据时,发现报错如下:java.lang.IllegalArgumentException:cannotwritexcontentforunknownvalueoftypeclassjava.math.BigD......
  • ELK系列(3) - Elasticsearch修改jvm参数
    方法Elasticsearch默认会配置1G的JVM堆的初始值和最大值,该jvm参数被配置在​​/config/jvm.options​​里:-Xms1g-Xmx1g如果只是个人开发小项目,可以把参数改小些,比如:-Xms512m......
  • 用python把word转成pdf
    需要使用python-docx模块pipinstallpython-docx然后函数主体importosimportdocx2pdfdefword_to_pdf(file_path):pdf_file=file_path.replace(".docx"......