Leetcode 刷题:Q58 Length of last word
2016-04-29 02:22
351 查看
Given a string s consists of upper/lower-case alphabets and empty space characters
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 =
return
public class Solution {
public int lengthOfLastWord(String s) {
//handle extreme conditions
if(s==null || s.length()==0)
return 0;
int count=0;
for(int i=s.length()-1;i>=0;i--){
if(s.charAt(i) != ' ')
count++;
if(s.charAt(i) == ' ' && count != 0)
return count;
}
return count;
}
}
' ', 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.
public class Solution {
public int lengthOfLastWord(String s) {
//handle extreme conditions
if(s==null || s.length()==0)
return 0;
int count=0;
for(int i=s.length()-1;i>=0;i--){
if(s.charAt(i) != ' ')
count++;
if(s.charAt(i) == ' ' && count != 0)
return count;
}
return count;
}
}
相关文章推荐
- Ruby入门之一(Ruby简介)
- OJ系列
- spring的bean的作用域
- Redis入门
- 重试C语言之C语言变量
- Hibernate之deleted object would be re-saved by cascade 异常的解决
- Intellij IdeaI快捷键
- 三、SpringBoot JPA 配置Hibernate
- [leetcode] 344. Reverse String 解题报告
- 使用AspectJ提供的注解方式实现aop
- bzoj 4551[Tjoi2016&Heoi2016]树
- [leetcode] 345. Reverse Vowels of a String 解题报告
- Django 文档自翻自用中文版(2.1-2.1.3)
- Python set集合
- java基础 -- 六哥
- activity的启动流程及框架
- 新建structs2 web应用及structs.xml常用基础配置
- 算法的时间复杂度的计算
- [log4j] 可用案例
- springMVC中使用注解@ResponseBody,@RequestBody传递json数据