58.Length of Last Word (最后一个单词长度)
2015-07-28 14:11
357 查看
描述: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 str1) {
String s = str1.replaceAll(" +"," ");
if(" ".equals(s)||s==null)
return 0 ;
else{
String []str = s.split(" ");
return str[str.length-1].length();
}
}
}
' ', 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 str1) {
String s = str1.replaceAll(" +"," ");
if(" ".equals(s)||s==null)
return 0 ;
else{
String []str = s.split(" ");
return str[str.length-1].length();
}
}
}
相关文章推荐
- 小韵游戏开发学习笔记——第二期:初识Unity3D
- ZooKeeper应用案例
- 《星途》项目使用相关工具(重要地址)
- Android APK 数据库升级简单例子
- 计算4!的值.java
- http、socket长连接和短连接
- MD5算法的C语言实现
- Mars老师的Android学习基本路线
- 使用IntelliJ IDEA 14和Maven创建java web项目
- button中title和image的相对位置
- Problem - 466C - Codeforces C. Number of Ways
- 最大熵模型
- java微信学习 接入
- Android 自定义ListView实现底部分页刷新与顶部下拉刷新,androidlistview
- 小韵游戏开发学习笔记——第一期:学习初衷
- VMware Vsphere高可用性(HA群集)
- codevs1064
- 基于maven的openjpa简单例子
- MySQL操作监控
- 安卓开源框架集合