Skip to content

Commit fb8ac1b

Browse files
authored
Create fish.md
1 parent 9c8e491 commit fb8ac1b

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

2018.12.3-leetcode58/fish.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
>给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度。
2+
>如果不存在最后一个单词,请返回 0 。
3+
>说明:一个单词是指由字母组成,但不包含任何空格的字符串。
4+
5+
```
6+
public static int lengthOfLastWord(String str){
7+
String trim = str.trim();
8+
if (str.matches("[a-zA-Z ]*") && !trim.isEmpty()) {
9+
int index = trim.lastIndexOf(' ');
10+
return index < 0 ? trim.length() : trim.length() - index - 1;
11+
}
12+
return 0;
13+
}

0 commit comments

Comments
 (0)