1. 原题链接

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

2. 题目要求

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

3. 解题思路

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

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

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

4. 代码实现

  1. public class LengthofLastWord59 {
  2. public static void main(String[] args) {
  3. String s = "";
  4. System.out.println(lengthOfLastWord(s));
  5. }
  6.  
  7. public static int lengthOfLastWord(String s) {
  8. if(s.length()==0) return 0;
  9. int slen = s.length() - 1;
  10. int res = 0;
  11.  
  12. // 过滤空格
  13. while (s.charAt(slen) == ' ' && slen != 0)
  14. slen--;
  15. // 计算单词长度
  16. while (s.charAt(slen) != ' ') {
  17. res++;
  18. if (slen == 0) break;
  19. slen--;
  20. }
  21. return res;
  22. }
  23. }

  

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. swagger使用二:swagger配置多个项目注释

    在项目中采用swagger测试接口,提供接口给其他人员都非常的方便. 在swagger默认配置中,默认只显示接口访问层中的注释,可是很多的参数说明都已经在实体层中了啊?(如下图)不可能再把实体层中的模 ...

  2. hdu-2620 Ice Rain---数论(取模运算规律)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2620 题目大意: 给出n和k求: 解题思路: kmodi=k-i*[k/i] ,所以=nk-(1*[ ...

  3. HDU 6370 Werewolf 【并查集】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6370 Werewolf Time Limit: 2000/1000 MS (Java/Others)   ...

  4. ON_COMMAND ON_MESSAGE ON_NOTIFY区别与联系

    ON_COMMAND是菜单和工具栏项处理消息的宏 ON_MESSAGE是处理自定义消息的宏 ON_NOTIFY 是控件向其父窗口发送消息处理的宏 对这几个消息的理解要先了解一下Window消息的背景. ...

  5. P1018 乘积最大(高精度加/乘)

    P1018 乘积最大 一道dp题目.比较好像的dp题目. 然而他需要高精度计算. 所以,他从我开始学oi,到现在.一直是60分的状态. 今天正打算复习模板.也就有借口解决了这道题目. #include ...

  6. asp.net mvc HtmlHelperExt EnumDropDownList

    public static class HtmlHelperExt { public static MvcHtmlString EnumDropDownList<TEnum>(this H ...

  7. 【转载】RETE算法研究

    本文转自:http://www.ibm.com/developerworks/cn/opensource/os-drools/ RETE算法是大多数规则引擎采用的一种模式匹配算法,比如开源的Drool ...

  8. JAVA中时间格式转换

    1.将任意日期格式的字符串转换为指定格式的字符串 //默认格式 String s1 = "20190110133236"; //给定格式 String s2 = "201 ...

  9. Spring知识点小结(二)

    一.配置非自定义的Bean(数据源DataSource模型) DBCP数据源:        导入dbcp的jar包:dbcp+pool+connector                代码实现:  ...

  10. 国产Linux下开发正式开工(deepin)

    配置开发环境 1.一般工具 在深度商店安装QQ,微信,安装一般软件WPS,Navicat数据库工具,文本编辑notepadqq. 影视娱乐爱奇艺,优酷,酷狗. 2.安装主要的开发环境 (1)c# 深度 ...