(java)Length of Last Word
来源:互联网 发布:linux shell exec 编辑:程序博客网 时间:2024/06/06 04:25
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
.
思路:先判断这个字符串中包不包含有效字符,是不是全是空格,如果不是,则将这个字符串按照空格分隔,返还最好一个字符串的长度
代码如下(已通过leetcode)
public class Solution {
public int lengthOfLastWord(String s) {
boolean isempty=true;
for(int i=0;i<s.length();i++) {
if(s.charAt(i)!=' ') {
isempty=false;
break;
}
}
if(isempty) return 0;
String[] ss= s.split(" ");
return ss[ss.length-1].length();
}
}
0 0
- Length of Last Word Java
- Length of Last Word (Java)
- (java)Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- 查找
- 文章标题
- 第9周项目2 - 对称矩阵压缩存储的实现与应用(2).cpp
- Hadoop安装过程遇到的坑:
- [Linux]ubuntu下共享文件夹密码设置和重置-smbpasswd -a user
- (java)Length of Last Word
- 117Populating Next Right Pointers in Each Node II
- Android捕获异常处理——UncaughtExceptionHandler
- ajax+json+Struts2实现list传递实例讲解
- 论文笔记 Visualizing and understanding convolutional networks
- 随机漫步模拟
- 第9周项目1 - 猴子选大王(数组版).cpp
- NODEJS 初学示例
- MFC中使用OLE/COM操作EXCEL的方法