Java for LeetCode 058 Length of Last Word
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.
解题思路:
从后往前遍历,用一个指针rightIndex表示第一次出现飞空格的位置,然后继续遍历,找出第一次出现空格的位置即可,JAVA实现如下:
static public int lengthOfLastWord(String s) {
int rightIndex=s.length()-1;
while(rightIndex>=0&&s.charAt(rightIndex)==' ')
rightIndex--;
if(rightIndex<0)
return 0;
for(int i=rightIndex-1;i>=0;i--)
if(s.charAt(i)==' ')
return rightIndex-i;
return rightIndex+1;
}
Java for LeetCode 058 Length of Last Word的更多相关文章
- Java [Leetcode 58]Length of Last Word
题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- [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
leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...
- 【leetcode】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(48)-Length of Last Word
题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...
- [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 ...
随机推荐
- 【前端】less学习
Less 是什么? Less is more,than CSS. Less就是搞笑高效编写和维护CSS的一种语法. 1.下载Koala考拉,一款国人编写的less开发器. 2.可以用Sublime T ...
- java导出txt文本
页面 项目结构 html代码 <html> </head> <body> <form action="down/downLoad" met ...
- 在cmd下编译一个简单的servlet时出现程序包javax.servlet不存在
由于servlet和JSP不是Java平台JavaSE(标准版)的一部分,而是Java EE(企业版)的一部分,因此,必须告知编译器servlet的位置. 解决“软件包 javax.servlet不存 ...
- Codeforces 593B Anton and Lines
LINK time limit per test 1 second memory limit per test 256 megabytes input standard input output st ...
- 微信公众平台开发接口PHP SDK完整版
<?php /* 方倍工作室 http://www.fangbei.org/ CopyRight 2015 All Rights Reserved */ define("TOKEN&q ...
- 一个令人蛋疼的 Microsoft.AspNet.FriendlyUrls
我一个项目都基本上做完了,结果部署到我服务器的时候结果一直报404 找不到 一看global.asax有个路由注册的代码 public static void RegisterRoutes(Route ...
- Web Service(1.8)
“基于 XMLWeb Service 的 Java API”(JAX-WS)通过使用注释来指定与 Web Service 实现相关联的元数据以及简化 Web Service 的开发.注释描述如何将 ...
- tcp MSL
MSL(最大分段生存期)指明TCP报文在Internet上最长生存时间,每个具体的TCP实现都必须选择一个确定的MSL值.RFC 1122建议是2分钟. TIME_WAIT 状态最大保持时间是2 * ...
- CentOS 7 使用经验(更新中)
首先说一下写这篇博客的初衷. 由于公司这一期的产品准备支持的环境有CentOS 7.MySql 5.6.Java 8.Tomcat 8等等,并且因为人员严重不足,我本月的开发任务在原有的基础上又加上了 ...
- Android SDK安装Android4.0“冰激淋三明治”(IceCreamSandwich)教程(转载)
昨天,Google举行了发布会,发布了Nexus Prime手机和Android4.0-IceCreamSandwich手机系统.作为Google旗下Android的最新版本手机系 统,Android ...