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.

思路:题目非常easy,没什么好说的,唯一要注意的就是后面的结尾可能非常多空格。

详细代码例如以下:

public class Solution {
public int lengthOfLastWord(String s) {
int len = 0;
boolean isEmpty = false;//从后往前数,直到不是空格
for(int i = s.length()-1; i > -1; i--){
if(s.charAt(i) != ' '){
len++;
isEmpty = true;
}else if(isEmpty){//倒数第二个空格出现
return len;
}
}
return len;
}
}

leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法的更多相关文章

  1. [LeetCode] 58. Length of Last Word 求末尾单词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  2. LeetCode: 58. Length of Last Word(Easy)

    1. 原题链接 https://leetcode.com/problems/length-of-last-word/description/ 2. 题目要求 给定一个String类型的字符串,字符串中 ...

  3. LeetCode 58. Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  4. Java [Leetcode 58]Length of Last Word

    题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...

  5. [leetcode]58. Length of Last Word最后一个词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  6. Leetcode 58 Length of Last Word 难度:0

    https://leetcode.com/problems/length-of-last-word/ int lengthOfLastWord(char* s) { int ans = 0; int ...

  7. Leetcode 58 Length of Last Word 字符串

    找出最后一个词的长度 class Solution { public: int lengthOfLastWord(string s) { , l = , b = ; while((b = s.find ...

  8. leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法

    First Missing Positive  Given an unsorted integer array, find the first missing positive integer. Fo ...

  9. leetCode 74.Search a 2D Matrix(搜索二维矩阵) 解题思路和方法

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

随机推荐

  1. BugKu-妹子的陌陌

    打开后看这张图片,先放winhex里面,文件头FFD8,是jpg图片.看文件尾并不是FFD9,所以binwalk分析一下. 发现有一个rar文件,然后用foremost分离.发现里面有个加密的rar文 ...

  2. WINDOWS批量替换不同文件夹下的相同文件

    今天帮媳妇解决的问题,记录一下,也许以后有用 例子: N个文件夹下有同一个文件(common.php),但是,现在对common.php文件进行了大量修改. 现在想用最新的common.php替换掉所 ...

  3. Java类和对象 详解(一)---写的很好通俗易懂---https://blog.csdn.net/wei_zhi/article/details/52745268

    https://blog.csdn.net/wei_zhi/article/details/52745268

  4. PS教程超级合辑【800+集爆款课】

    第1章 导读——推荐大家到网易云课堂学习购买(本博文仅为个人学习笔记)https://study.163.com/course/courseMain.htm?courseId=1442008& ...

  5. camelot工具进行pdf表格解析重建

    camelot内置生成html文件的方法,但表格数据转化成pandas.dataframe的过程中,丢失了跨行跨列的结构信息,故生成html的表格无跨行跨列结构. 于是我在输出部分选择直接手写html ...

  6. 菜鸟之路——机器学习之SVM分类器学习理解以及Python实现

    SVM分类器里面的东西好多呀,碾压前两个.怪不得称之为深度学习出现之前表现最好的算法. 今天学到的也应该只是冰山一角,懂了SVM的一些原理.还得继续深入学习理解呢. 一些关键词: 超平面(hyper ...

  7. mybatis maven 代码生成器(mysql)

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  8. Struts2 改变语言状态

    只要在请求中增加 request_locale=en_US 参数,就可以实现语言的切换,内部由拦截器实现

  9. Robot Framework通过Python SMTP进行email收发测试。

    工作中需要对发送的邮件进行过滤,方法基本属于ACL控制,即查看“源/目的”邮件地址,邮件标题,邮件正文,邮件附件等进行过滤. 所以需要先模拟一下用Python能否达到邮件Client,Server的功 ...

  10. Java EE 学习:使用 idea2017 搭建 SSM 框架

    需要准备的环境: idea 2017.1 jdk1.8 Maven 3.3.9 请提前将idea与Maven.jdk配置好,本次项目用的都是比较新的 步骤: 一.首先使用idea新建一个Maven w ...