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. 题目要求 给定一个字符串数组,让你求出该数组中所 ...
随机推荐
- json查看器
jsonview: http://www.bejson.com/jsonviewernew/
- MySQL错误问题
启动Tomcat的时候报错:no suitable driver,MySql更新使用com.mysql.cj.jdbc.Driver,废弃老的com.mysql.jdbc.Driver驱动,需要将D: ...
- PHP------练习------投票
练习------投票 一 .题目要求: 二 .做法 [1]建立数据库 表名:diaoyantimu 表名: diaoyanxuanxiang [2]封装类文件 <?php class DBDA ...
- Codeforces Round #527 (Div. 3) D1. Great Vova Wall (Version 1) 【思维】
传送门:http://codeforces.com/contest/1092/problem/D1 D1. Great Vova Wall (Version 1) time limit per tes ...
- tomcat配置APR
转载 Windows下配置Tomcat的Apr(包括Https) tomcat bio nio apr 模式性能测试与个人看法 一.windows 下配置Tomcat的APR: 1.到Apache ...
- STS使用git下载项目代码
在自己的eclipse 上安装git 插件,一般都自带了现在. 4.选择Clone URI 5.下一步输入刚才的复制的路劲,填写自己的github 账户名密码即可 6.选择要克隆的分支 7.设置本地g ...
- AD9516锁相环功能外接环路滤波器的设计与分析
- Oracle中case的第二种用法
procedure P_GetProVerSingInfo_2018(varFileID in varchar2, p_cr1 out refcontent, p_cr2 out refcontent ...
- 《Linux 学习》01---redis安装, 并使用Redis Desktop Manager 连接
一.环境简介: linux 系统:centos 7.X 二.安装大纲: 1.下载安装包 2.安装 3.统一管理redis 配置文件 4.编辑redis配置文件,设置常用的功能 5.(1)命令启动,连接 ...
- Mybatis 原始dao CRUD方法
用到的相关jar包及所用版本如下: 其中的Mybatis可以到github.com的网站下载 <project xmlns="http://maven.apache.org/POM/4 ...