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. QuantLib金融库的简易安装

    最近在弄毕设,研究关于固定收益债券定价方面的知识,需要使用到QuantLib这个Python金融库,但是这是一个C++编译的库,官网也只给出了源代码,安装起来十分繁琐,所以在网上找了一个简易的安装方法 ...

  2. 理解Underscore中的flatten函数

    最近是在所在实习公司的第一个sprint,有个朋友又请假了,所以任务比较重,一直这么久都没怎么更新了,这个周末赖了个床,纠结了一会儿决定还是继续写这个系列,虽然比较乏味,但是学到的东西还是很多的. 之 ...

  3. arcgis server 9.3 查看地图服务时出现"No Content"错误

    问题描述: 使用ArcGIS Server Manager的Add new service功能发布一个服务.然后启动服务 用浏览器访问该服务的地址http://localhost/ArcGIS/ser ...

  4. SOJ4459 skysky's game(贪心+优先队列)

    天天最近迷上了天天爱消除游戏,现在他觉得这个游戏已经没有意思了.所以他发明一个新的消除游戏.有n堆糖果,每一个糖果有一个重量w,天天每次都选择两个糖果合并为一个糖果,新的糖果的重量等于这两个糖果的重量 ...

  5. 人类主动探索地外文明(METI)活动正在进行中

                  请看下图:            这是位于俄罗斯克里米亚境内的行星际深空通讯雷达(口径70米,2-300GHz,建造于1978年)的外观.借助该雷达的强大发射功率,有关国际 ...

  6. UVa 1635 - Irrelevant Elements(二项式系数 + 唯一分解定理)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. 1spring注解:@Configuration,@Bean,@ComponentScan(),@Scope

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

  8. Linux实用指令(1):运行级别&找回密码&帮助指令&文件目录类

    指令运行级别: 0 :关机 1 :单用户[找回丢失密码] 2:多用户状态没有网络服务 3:多用户状态有网络服务 4:系统未使用保留给用户 5:图形界面 6:系统重启   常用运行级别是3和5 ,要修改 ...

  9. 关注磁盘的两个指标: IOPS 和传输带宽(吞吐量)

    ㈠ IOPS                磁盘的 IOPS.也就是每秒能进行多少次IO        那么.如何才算一次IO呢?        其实.这是个定义很混乱的问题        因为.系统 ...

  10. ICT测试点是干什么的, 怎么设置!

    简单理解:ICT类似如万用表,只是把表笔换成了测试针.那么问题就简单了,一颗普通的RLC元件,都必须有两个测试点才能够测试,当然同一个网络共用的节点用一个测试点就可以了. 详细描述: PCB设计时要看 ...