Leet Code OJ 58. Length of Last Word [Difficulty: Easy]
题目:
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]相关推荐
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- LeetCode刷题记录9——58. Length of Last Word(easy)
LeetCode刷题记录9--58. Length of Last Word(easy) 目录 LeetCode刷题记录9--58. Length of Last Word(easy) 题目 语言 思 ...
最新文章
- Database项目中关于Procedure sp_refreshsqlmodule_internal的错误
- springboot 异步不生效
- python中的类属性和类方法_python面向对象之类属性和类方法的使用和实例
- 7.Eclipse中创建新Maven项目
- 变异函数 python_使用Python进行变异测试
- @builder注解_SpringBoot 整合SpringSecurity示例实现前后分离权限注解+JWT登录认证
- 按一个按钮会随机死人_《饥荒》那些年坑爹的随机地图,最后一个简直笑死人...
- 男孩读计算机好还是铁路学校好,中专学计算机好还是铁路好?
- LoadRunner解决“服务器正在运行中”方法
- 将iTunes音乐传输到USB驱动器以在汽车中收听
- Unity AudioMixer效果器设置
- html中圆角和颜色透明度的属性与使用
- 个人身份认证接口介绍
- 周转时间和带权周转时间的计算
- 服务器硬盘接口有哪些种类
- java里部分文件全选怎么整_选定全部文件的快捷键-怎么全选文件夹的文件-文件...
- 《图解HTTP》(四)更安全的HTTPS、用户认证
- 角点检测汇总:Harris角点及Shi-Tomasi角点检测
- 中文文本特征提取—流程
- Leetcode 37:解数独(超详细的解法!!!)
热门文章
- BST(Binary Search Tree 二叉查找树模版)
- SkinMagic使用后按钮加自定义图标或菜单GetMneu返回NULL的解决方法
- cocos2d-x初探学习笔记(1)--HelloWorld
- linux下使用python操作mysql
- 音视频技术开发周刊 | 230
- Netflix媒体数据库:媒体时间线数据模型
- LiveVideoStack线上交流分享 ( 二 ) —— 爱奇艺版权保护技术与维权实践
- 展望2018,多媒体开发新趋势
- 数据结构与算法之二叉树的序列化和反序列化及判断一棵树是否为平衡二叉树
- Java基础之多态深入解析