-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLengthofLastWord.java
More file actions
46 lines (41 loc) · 1.28 KB
/
LengthofLastWord.java
File metadata and controls
46 lines (41 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package easy;
/**
* 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.
* <p>
* Note: A word is defined as a character sequence consists of non-space characters only.
* <p>
* 给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度。
* 如果不存在最后一个单词,请返回 0 。
* <p>
* 说明:一个单词是指由字母组成,但不包含任何空格的字符串。
* <p>
* Created by kaming on 2018/9/10.
*/
public class LengthofLastWord {
/**
* Example:
*
* Input: "Hello World"
* Output: 5
*/
public static void main(String[] args){
int exp1 = lengthOfLastWord(" ");
System.out.println(exp1);
}
private static int lengthOfLastWord(String s) {
int wordLength;
if (s == null || s.length() == 0){
wordLength = 0;
}else{
String[] wordArr = s.split(" ");
if (wordArr.length == 0 ){
wordLength = 0;
}else{
String word = wordArr[wordArr.length - 1];
wordLength = word.length();
}
}
return wordLength;
}
}