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 ...
随机推荐
- 【Matplotlib】设置刻度(1)
刻度设置 参考文档: xticks 命令 yticks 命令 以xticks为例: matplotlib.pyplot.xticks(*args, **kwargs) 获取或者设置当前刻度位置和文本的 ...
- BZOJ-2748 音量调节 DP+背包(脑残)
水DP,一开始竟然想错了...水了半天....真可怕 2748: [HAOI2012]音量调节 Time Limit: 3 Sec Memory Limit: 128 MB Submit: 1156 ...
- UVA 1149 Bin Packing
传送门 A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the sa ...
- android anr分析方法
目录(?)[+] 案例1关键词ContentResolver in AsyncTask onPostExecute high iowait 案例2关键词在UI线程进行网络数据的读写 一:什么是AN ...
- App接口简介
- Laravel教程 三:视图变量传递和Blade
Laravel教程 三:视图变量传递和Blade 此文章为原创文章,未经同意,禁止转载. Blade 上一篇我们简单地说了Router,Views和Controllers的工作流程,这一次我就按照上一 ...
- Visual Studio Online Integrations-Planning
原文:http://www.visualstudio.com/zh-cn/explore/vso-integrations-directory-vs
- ubuntu显示桌面的快捷键,以及修改方法
在ubuntu下面,快速显示桌面,你可以这样做. 1,ctrl+alt+d (默认的) 2,alt+tab 可以切换到桌面 但是我想把它修改成和windows一样的,我该怎么做呢? 其实很简单. 系统 ...
- 细说HTTP上篇
HTTP概述 每天,都有数以亿万计的JPEG图片.HTML页面.文本文件.MPEG电影.WAV音频文件.Java小程序和其他资源在因特网上游弋.HTTP可以从遍布全世界的Web服务器上将这些信息快速. ...
- Sublime Text2 快捷键 (MAC版)
工具是人的延伸,可以把人变得更聪明更强大,人类正是学会了使用工具,才创造出现在的文明.作为程序员,工具开发.使用是其能力的重要体现,业内的大牛都是造工具的好手.目前身边很多人都在用sublime te ...