LeetCode: 58. Length of Last Word(Easy)
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)的更多相关文章
- Leetcode 之Length of Last Word(37)
扫描每个WORD的长度并记录即可. int lengthOfLast(const char *s) { //扫描统计每个word的长度 ; while (*s) { if (*s++ != ' ')/ ...
- Leetcode 之Length of Last Word(38)
做法很巧妙.分成左右两个对应的部分,遇到左半部分则入栈,遇到右半部分则判断对应的左半部分是否在栈顶.注意最后要判断堆栈是否为空. bool isValid(const string& s) { ...
- [LeetCode] 58. Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法
Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space charact ...
- LeetCode 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- Java [Leetcode 58]Length of Last Word
题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...
- [leetcode]58. Length of Last Word最后一个词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 【LeetCode】- Length of Last Word(最后一个单词的长度)
[ 问题: ] Given a string s consists of upper/lower-case alphabets and empty space characters ' ', retu ...
- LeetCode:14. Longest Commen Prefix(Easy)
1. 原题链接 https://leetcode.com/problems/longest-common-prefix/description/ 2. 题目要求 给定一个字符串数组,让你求出该数组中所 ...
随机推荐
- gulp使用方法总结
gulp是用于前端构建的基于文件流的一套工具.可以用于压缩.编译.合并.检查文件等操作.可以节省大量的用于繁琐重复操作的人力.最开始就是安装gulp工具了,在命令行中切换到工作的文件目录下,安装gul ...
- 当当网-前端project师測试题
前端project师測试题(笔试时间20分钟.面试时间20分钟) 一.笔试 1.基础问题 (1)前端页面有哪三层构成,各自是什么? ...
- 转:Json序列化和反序列化
JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍 ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介 ...
- AutoComplete的使用方法
百度 酷狗,反正使用搜索功能时,都会看到类似于图一这种自动补全的功能,灰常的方便,今天做一个项目,刚好要加这个功能,于是一通百度之后,总算做出来,源代码在文章末尾会提供下载.还有,我这个是参考了网上的 ...
- maven下载依赖jar包失败处理方法--下载jar包到本地并安装到maven仓库中
所有maven依赖jar包地址:https://repo1.maven.org/maven2/org/apache/ 1. 安装jar包失败报错: The following artifacts co ...
- MacBook常用快捷键
MacBook常用快捷键: 1. 窗口操作: cmd+n 新建一个窗口/文件. cmd+m 窗口最小化. cmd+w 关闭当前窗口/文件. 2. 程序操作: cmd+q 退出当前程序,后台不运行该程序 ...
- 自定义属性之LinearLayout ImageView TextView模拟图片文字按钮
一.资源文件: 1.文字选择器: <?xml version="1.0" encoding="utf-8"?> <selector xmlns ...
- 如何安装zip格式的MySQL
1.MySQL安装文件分为两种,一种是msi格式的,一种是zip格式的.如果是msi格式的可以直接点击安装,按照它给出的安装提示进行安装(相信大家的英文可以看懂英文提示),一般MySQL将会安装在C: ...
- tomcat启动后报错Bad version number in .class file (unable to load class oracle.jdbc.OracleDriver)
对于tomcat启动后报错: 错误原因:tomcat使用的jdk和eclipce的编译用的jdk版本不同. 解决办法: 1.首先确定tomcat的jdk版本: 2.点开tomcat查看jdk版本. 使 ...
- winform 里 如何实现文件上传
看了网上写的 用webclient类的uploadfile方法, 我在本地建立了个webform,winform窗体, 现在可以本地实现文件传递,可以选择文件传到d:\temp路径下,但怎们传到服务 ...