58. 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.

解题

思路

这道题是java太好写了吗??

代码

/**
* Created with IntelliJ IDEA
* Date: 2016/3/19
* Time: 16:11
* User: ThinerZQ
* GitHub: <a>https://github.com/ThinerZQ</a>
* Blog: <a>http://www.thinerzq.me</a>
* Email: 601097836@qq.com
*/
public class LengthofLastWord_58 {
public static void main(String[] args) {
}
public int lengthOfLastWord(String s) {
if (s == null || s.trim().length() == 0) {
return 0;
} else {
String[] strings = s.split(" ");
return strings[strings.length - 1].length();
}
}
}
坚持原创技术分享,您的支持将鼓励我继续创作!

热评文章

Fork me on GitHub