题目:
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.

翻译:
给定一个字符串,它是由大小写字母和空格组成的,返回字符串中最后一个单词的长度。
如果最后一个单词不存在,就返回0.
提示:一个单词是指一个只由非空格的字符组成的字符串序列。

分析:
有一点题目中没有说明的是,假如最后是空格的话,如何处理。经过测试,发现最后是空格的话,还是返回上一个单词的长度,并不会清零。

代码:

public class Solution {public int lengthOfLastWord(String s) {char[] arr=s.toCharArray();int count=0;boolean needClean=false;for(int i=0;i<arr.length;i++){if(arr[i]!=' '){if(needClean){count=0;needClean=false;}count++;}else{needClean=true;}}return count;}
}

Leet Code OJ 58. Length of Last Word [Difficulty: Easy]相关推荐

  1. Leet Code OJ 119. Pascal's Triangle II [Difficulty: Easy]

    题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...

  2. Leet Code OJ 8. String to Integer (atoi) [Difficulty: Easy]

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  3. Leet Code OJ 203. Remove Linked List Elements [Difficulty: Easy]

    题目: Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 ...

  4. Leet Code OJ 191. Number of 1 Bits [Difficulty: Easy]

    题目: Write a function that takes an unsigned integer and returns the number of '1' bits it has (also ...

  5. Leet Code OJ 328. Odd Even Linked List [Difficulty: Easy]

    题目: Given a singly linked list, group all odd nodes together followed by the even nodes. Please note ...

  6. Leet Code OJ 21. Merge Two Sorted Lists [Difficulty: Easy]

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  7. Leet Code OJ 168. Excel Sheet Column Title [Difficulty: Easy]

    题目: Given a positive integer, return its corresponding column title as appear in an Excel sheet. For ...

  8. Leet Code OJ 171. Excel Sheet Column Number [Difficulty: Easy]

    题目: Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, r ...

  9. LeetCode刷题记录9——58. Length of Last Word(easy)

    LeetCode刷题记录9--58. Length of Last Word(easy) 目录 LeetCode刷题记录9--58. Length of Last Word(easy) 题目 语言 思 ...

最新文章

  1. Database项目中关于Procedure sp_refreshsqlmodule_internal的错误
  2. springboot 异步不生效
  3. python中的类属性和类方法_python面向对象之类属性和类方法的使用和实例
  4. 7.Eclipse中创建新Maven项目
  5. 变异函数 python_使用Python进行变异测试
  6. @builder注解_SpringBoot 整合SpringSecurity示例实现前后分离权限注解+JWT登录认证
  7. 按一个按钮会随机死人_《饥荒》那些年坑爹的随机地图,最后一个简直笑死人...
  8. 男孩读计算机好还是铁路学校好,中专学计算机好还是铁路好?
  9. LoadRunner解决“服务器正在运行中”方法
  10. 将iTunes音乐传输到USB驱动器以在汽车中收听
  11. Unity AudioMixer效果器设置
  12. html中圆角和颜色透明度的属性与使用
  13. 个人身份认证接口介绍
  14. 周转时间和带权周转时间的计算
  15. 服务器硬盘接口有哪些种类
  16. java里部分文件全选怎么整_选定全部文件的快捷键-怎么全选文件夹的文件-文件...
  17. 《图解HTTP》(四)更安全的HTTPS、用户认证
  18. 角点检测汇总:Harris角点及Shi-Tomasi角点检测
  19. 中文文本特征提取—流程
  20. Leetcode 37:解数独(超详细的解法!!!)

热门文章

  1. BST(Binary Search Tree 二叉查找树模版)
  2. SkinMagic使用后按钮加自定义图标或菜单GetMneu返回NULL的解决方法
  3. cocos2d-x初探学习笔记(1)--HelloWorld
  4. linux下使用python操作mysql
  5. 音视频技术开发周刊 | 230
  6. Netflix媒体数据库:媒体时间线数据模型
  7. LiveVideoStack线上交流分享 ( 二 ) —— 爱奇艺版权保护技术与维权实践
  8. 展望2018,多媒体开发新趋势
  9. 数据结构与算法之二叉树的序列化和反序列化及判断一棵树是否为平衡二叉树
  10. Java基础之多态深入解析