1. 原题链接

https://leetcode.com/problems/length-of-last-word/description/

2. 题目要求

给定一个String类型的字符串,字符串中可能含有空格‘  ’。从字符串的末尾开始查找,找到第一个单词,该单词中间不能有空格,并返回其长度。

3. 解题思路

首先判断该字符串是否为空,为空直接返回 0;

否则,从尾部向前查找,使用两个while循环,第一个while循环用于过滤空格,当该位置不为空格时,跳出while循环;

然后进入第二个while循环,依次向前查找,用使用累加器进行记录单词长度。直到当前位置为空格时,跳出while循环,并返回单词长度。

4. 代码实现

public class LengthofLastWord59 {
public static void main(String[] args) {
String s = "";
System.out.println(lengthOfLastWord(s));
} public static int lengthOfLastWord(String s) {
if(s.length()==0) return 0;
int slen = s.length() - 1;
int res = 0; // 过滤空格
while (s.charAt(slen) == ' ' && slen != 0)
slen--;
// 计算单词长度
while (s.charAt(slen) != ' ') {
res++;
if (slen == 0) break;
slen--;
}
return res;
}
}

  

LeetCode: 58. Length of Last Word(Easy)的更多相关文章

  1. Leetcode 之Length of Last Word(37)

    扫描每个WORD的长度并记录即可. int lengthOfLast(const char *s) { //扫描统计每个word的长度 ; while (*s) { if (*s++ != ' ')/ ...

  2. Leetcode 之Length of Last Word(38)

    做法很巧妙.分成左右两个对应的部分,遇到左半部分则入栈,遇到右半部分则判断对应的左半部分是否在栈顶.注意最后要判断堆栈是否为空. bool isValid(const string& s) { ...

  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. leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法

    Length of Last Word  Given a string s consists of upper/lower-case alphabets and empty space charact ...

  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. Java [Leetcode 58]Length of Last Word

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

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

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

  8. 【LeetCode】- Length of Last Word(最后一个单词的长度)

    [ 问题: ] Given a string s consists of upper/lower-case alphabets and empty space characters ' ', retu ...

  9. LeetCode:14. Longest Commen Prefix(Easy)

    1. 原题链接 https://leetcode.com/problems/longest-common-prefix/description/ 2. 题目要求 给定一个字符串数组,让你求出该数组中所 ...

随机推荐

  1. 随机以及时间相关函数——C语言描述

    随机相关的函数 头文件 stdlib.h 相关函数 :rand .srand rand( rand C++ Reference ) 函数声明:int rand( void ); rand函数返回一个位 ...

  2. IntelliJ IDEA 2017 完美注册方法及破解方法

    本文使用破解方式注册. 下载破解文件JetbrainsCrack-2.6.2.jar 下载地址: http://idea.lanyus.com/ 开始破解 一.将下载的 JetbrainsCrack- ...

  3. 一个关于JSON的异常,获取List对象失败的。。。

    重要的事情放在最前面,,以后不管遇到什么异常都一定要把异常读懂再想办法怎么解决,把异常读懂,异常读懂...... 这个异常我记得以前遇到过,而且好像已经做了笔记,,,,,今天翻了一下竟然没有,,,,, ...

  4. HDU 6386 Age of Moyu 【BFS + 优先队列优化】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6386 Age of Moyu Time Limit: 5000/2500 MS (Java/Others ...

  5. iview(DatePicker)时间转入后台少一天 解决方案

    后台注解 前台: 加个事件 @on-change @on-change="getStartTime" getStartTime(starTime) { this.leaveReco ...

  6. CSU 1726: 你经历过绝望吗?两次!(bfs+优先队列)

    传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1726 1726: 你经历过绝望吗?两次! Submit Page    Summar ...

  7. 对于dequeueReusableCellWithIdentifier:的理解

      Table Data Source Methods中的一个必要实现的方法tableView: cellForRowAtIndexPath: 中经常会包含一段代码: static NSString  ...

  8. HTML5零散知识点总结

    1.产生ioc图标的网站: http://www.bitbug.net/ 链接ioc图标: <link rel="shortcut icon" type="imag ...

  9. React Native开发中自动打包脚本

    React Native开发中自动打包脚本 在日常的RN开发中,我们避免不了需要将我们编写的代码编译成安装包,然后生成二维码,供需要测试的人员扫描下载.但是对于非原生的开发人员来说,可能不知如何使用X ...

  10. input和div模仿select,带输入提示

    有时候我们需要select和input的结合体,即可以使用下拉框,同时也可以用来输入,输入的同时显示可选的下拉选项 先上html代码 <div class="input-group i ...