Length of Last Word leetocde java
题目:
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,说明最后一个单词已经被计数了,所以可以返回了。
代码如下:
1 public int lengthOfLastWord(String s) {
2 if (s == null || s.length() == 0)
3 return 0;
4
5 int len = s.length();
6 int count = 0;
7 for (int i = len - 1; i >= 0; i--) {
8 if (s.charAt(i) != ' ') {
9 count++;
}
if(s.charAt(i)==' '&&count != 0){
return count;
}
}
return count;
}
当然这道题也能用投机取巧的方法,用split函数把字符串按照空格分隔好,返回最后那个就行。。。
代码如下:
public int lengthOfLastWord(String s) {
String[] a = s.split(" ");
if(a == null || a.length == 0)
return 0;
return a[a.length-1].length();
}
Length of Last Word leetocde java的更多相关文章
- Java for LeetCode 058 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 ...
- 422. Length of Last Word【LintCode java】
Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...
- [LeetCode] Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- lintcode:Length of Last Word 最后一个单词的长度
题目: 最后一个单词的长度 给定一个字符串, 包含大小写字母.空格' ',请返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 样例 给定 s = "Hello World& ...
- 58. Length of Last Word
题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...
- LeetCode——Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- LeetCode OJ: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 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
随机推荐
- 异步任务 -- FutureTask
任务提交 之前在分析线程池的时候,提到过 AbstractExecutorService 的实现: public Future<?> submit(Runnable task) { if ...
- Java异常处理中的恢复模型
异常处理理论上有两种基本模型.Java支持终止模型,在这种模型中,假设错误非常关键,以至于程序无法返回到异常发生的地方继续执行.一旦异常被抛出,就表明错误已无法挽回,也不能回来继续执行.长久以来,尽管 ...
- 移动端h5下ul实现横向滚动css代码
html代码: <ul id="category"> <li>品牌团</li> <li>美体个护</li> <li ...
- python中的super( test, self).__init__()
python中的super( test, self).__init__() 对继承自父类的属性进行初始化 首先找到test的父类(比如是类A),然后把类test的对象self转换为类A的对象,然后“被 ...
- python开发_tkinter_修改tkinter窗口的红色图标'Tk'
学过java的swing可能知道,在创建一个窗口的时候,窗口的左上角是一个咖啡图标 如下图所示: 在python中,tkinter模块生成的窗口左上角是一个:Tk字样的图标(Tk为tkinter的缩写 ...
- XMOJ 1133: 膜拜大牛 计算几何/两圆相交
1133: 膜拜大牛 Time Limit: 1 Sec Memory Limit: 131072KiBSubmit: 9619 Solved: 3287 题目连接 http://acm.xmu. ...
- c# 对字符串反序列成匿名对象
/// <summary> /// 需求单列表 /// </summary> /// <param name="model"></para ...
- spring---aop(10)---Spring AOP中AspectJ
写在前面 在之前的文章中有写到,Spring在配置中,会存在大量的切面配置.然而在很多情况下,SpringAOP 所提供的切面类真的不是很够用,比如想拦截制定的注解方法,我们就必须扩展DefalutP ...
- oracle case when exists()
用法如下: select case when exists(select 1 from t_test c where c.name = 'zhangsan' and c.age = 23 ) ...
- mysql学习心得转
http://www.cnblogs.com/lyhabc/p/3691555.html