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. 以ADO形式操作mysql数据库

    首先得需要一个连接mysql的helper类: public class MySqlHelper { #region [ Connection ] public static string conne ...

  2. EF Core中Key属性相同的实体只能被跟踪(track)一次

    在EF Core的DbContext中,我们可以通过DbContext或DbSet的Attach方法,来让DbContext上下文来跟踪(track)一个实体对象,假设现在我们有User实体对象,其U ...

  3. ObjC之RunTime(下)

    之前通过学习官方文档对runtime有了初步的认识,接下来就要研究学习runtime到底能用在哪些地方,能如何改进我们的程序. 本文也可以从icocoa浏览. Swizzling Swizzling可 ...

  4. MySQL学习之变量

    变量 MySQL本质是一种编程语言,需要很多变量来保存数据,mysql中很多的属性控制都是通过MySQL中固有的变量来实现的. 系统变量 系统内部定义的变量,系统变量针对的是所有用户(MySQL客户端 ...

  5. sql 复杂查询 以teacher student course多对多关系为例

    数据库表和值 /* SQLyog Ultimate v8.32 MySQL - 5.7.17-log : Database - course_dbms ************************ ...

  6. [SHELL]软件管理

  7. 【若泽大数据实战第二天】Linux命令基础

    Linux基本命令: 查看IP: ifconfig 或者 hostname -i(需要配置文件之后才可以使用) ipconfig(Windows) 关闭防火墙: Service iptables st ...

  8. Eclipse易卡死

    在用eclipse编辑项目的时候,经常卡死,经过查询知道原来是我的JDK和eclipse版本对应的不好,我们都知道,eclipse的环境需要配置. 当时情况是这样的 2.容易出现卡死或者如图所示的情况 ...

  9. nginx通过upstream实现负载均衡

    随着业务和用户不断增加,单台服务器无法满足业务需求,产生服务器集群的场景.为了能充分利用服务器集群,最理想的方式就是整个集群的利用率都很平均且稳定在理想值范围内. 负载均衡(Load Balance) ...

  10. Mongodb从库配置

    1. 先以master方式启动mongodb 2. 导入主库的数据文件:/data/mongodb-3.0.12/bin/mongorestore /data/tmp/mongodbbak/ 3. 关 ...